- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
self.window.backgroundColor=[UIColorwhiteColor];
UIViewController *v=[[UIViewControlleralloc]init];
self.window.rootViewController=v;
[self.windowmakeKeyAndVisible];
//创建数组,内容是分段控件的各分区标题
NSArray *ns=[[NSArrayalloc]initWithObjects:@"enter",@"come",@"system",nil];
//创建分段控件
UISegmentedControl *segment=[[UISegmentedControlalloc]initWithItems:ns];
//设置控件大小
segment.frame=CGRectMake(30,40, 200, 50);
//初始选中第一位置的区
[segment setSelectedSegmentIndex:1];
//给控件设置目标事件
[segment addTarget:selfaction:@selector(visibel:)forControlEvents:UIControlEventValueChanged];
//创建标签及大小
label=[[UILabelalloc]initWithFrame:CGRectMake(250,40, 70, 50)];
label.backgroundColor=[UIColorgreenColor];
[self.windowaddSubview:segment];
[self.windowaddSubview:label];
//UIViewController *v=[[UIViewController alloc]init];
return YES;
}
-(void)visibel:(UISegmentedControl *)sender
{
//测试控件各分区的索引位置
NSLog(@"%d",(int)[senderselectedSegmentIndex]);
//让标签显示的文本等于当前选中的控件分区标题
label.text=[sendertitleForSegmentAtIndex:[sender selectedSegmentIndex]];
//文本方向置中
label.textAlignment=NSTextAlignmentCenter;
}