宽容他人,放过自己。

UISegmentedControl_分段控件_相当于集成多个button

Posted on By anchoriteFili

132041534418753.png

132036401446200.png

#pragma mark 创建UISegmentedControl
    NSArray *items = [NSArray arrayWithObjects:@"首页",@"列表",@"收藏", nil];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:items];
    segment.frame = CGRectMake(0, 30, 320, 30);
    segment.tintColor = [UIColor cyanColor];
//    设置初始时选取的item的坐标
    segment.selectedSegmentIndex = 0;
//    在1坐标处插入我的空间item
    [segment insertSegmentWithTitle:@"我的空间" atIndex:1 animated:YES];
    [self.view addSubview:segment];
    [segment release];

UISegmentedControl事件的触发

self.brightSegment.selectedSegmentIndex = 0;
    [self.brightSegment addTarget:self action:@selector(hehe:) forControlEvents:UIControlEventValueChanged];

#pragma mark 触发事件
- (void)hehe:(UISegmentedControl *)sender {
    
    if (sender.selectedSegmentIndex == 0) {
        NSLog(@"hehehe");
    } else if (sender.selectedSegmentIndex == 1) {
        NSLog(@"hahahah");
    }
}