iOS开篇——UI之UISegmentedControl (分段选择器)

创建分段选择器

    UISegmentedControl * sc = [[UISegmentedControl alloc]initWithFrame:CGRectMake(50, 100, 200, 30)];
    
    [sc insertSegmentWithTitle:@"第一页" atIndex:0 animated:YES];
    [sc insertSegmentWithTitle:@"第二页" atIndex:1 animated:YES];
    [sc insertSegmentWithTitle:@"第三页" atIndex:2 animated:YES];
    
    [sc addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:sc]; 

实现点击方法

- (void)onClick:(UISegmentedControl *)sc {
    if (sc.selectedSegmentIndex == 0) {
        [sc removeSegmentAtIndex:0 animated:YES];
    }else if(sc.selectedSegmentIndex == 1){
        [sc insertSegmentWithTitle:@"礼拜" atIndex:0 animated:YES];
    }
}
//根据传来的sc的index值判断执行的方法
原文地址:https://www.cnblogs.com/gwkiOS/p/4990251.html