Objective--C UISegmentedControl 交通灯

来源:互联网 发布:linux shell exec 编辑:程序博客网 时间:2024/05/29 15:31

只是简单的点击变化背景颜色,主要记录UISegmentedControl的使用


在viewDidLoad方法下

NSArray *segArr = [[NSArrayalloc] initWithObjects:@"red",@"yellow",@"green",nil];

    UISegmentedControl *seg = [[UISegmentedControlalloc] initWithItems:segArr];

    seg.frame = CGRectMake(100, 100, 200, 50);

    seg.backgroundColor = [UIColorcyanColor];

    

    [seg addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventValueChanged];

    

    [self.viewaddSubview:seg];

    [seg release];


显示的是,可以进行点击



// 点击事件的实现

- (void)click:(UISegmentedControl *)seg{


    NSInteger i = seg.selectedSegmentIndex;

    

    switch (i) {

        case 0:

            self.view.backgroundColor = [UIColorredColor];

            break;

        case 1:

            self.view.backgroundColor = [UIColoryellowColor];

            break;

        case 2:

            self.view.backgroundColor = [UIColorgreenColor];

            break;

            

        default:

            break;

    }

    

}




0 0
原创粉丝点击