您的位置:首页 > 移动开发 > Objective-C

Objective--C UISegmentedControl 交通灯

2015-12-17 19:20 399 查看
只是简单的点击变化背景颜色,主要记录UISegmentedControl的使用

在viewDidLoad方法下

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

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

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

seg.backgroundColor = [UIColor
cyanColor];

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

[self.view
addSubview:seg];

[seg release];

显示的是

,可以进行点击

// 点击事件的实现

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

NSInteger i = seg.selectedSegmentIndex;

switch (i) {

case
0:

self.view.backgroundColor = [UIColor
redColor];

break;

case
1:

self.view.backgroundColor = [UIColor
yellowColor];

break;

case
2:

self.view.backgroundColor = [UIColor
greenColor];

break;

default:

break;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: