建立分段選擇器
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