iOS UISegmentedControl 的简单使用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    NSArray *array=@[@"web",@"看看",@"视频",@"音乐"];
    UISegmentedControl *segmentC=[[UISegmentedControl alloc]initWithItems:array];
    segmentC.segmentedControlStyle=UISegmentedControlStyleBar;
    segmentC.frame=CGRectMake(60, 100, 200, 40);
    segmentC.selectedSegmentIndex=0;
    //设置tintColor
    //Default tintColor is nil. Only used if style is UISegmentedControlStyleBar or UISegmentedControlStyleBezeled
    segmentC.tintColor=[UIColor redColor];
    [segmentC addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];
    [self.window addSubview:segmentC];
    [self.window makeKeyAndVisible];
    return YES;
}


-(void)changeSegment:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 0:
            NSLog(@"第1个");
            break;
        case 1:
            NSLog(@"第2个");
            break;
        case 2:
            NSLog(@"第3个");
            break;
        case 3:
            NSLog(@"第4个");
            break;
            
        default:
            break;
    }
}

你可能感兴趣的:(iOS UISegmentedControl 的简单使用)