iOS分段控制器 - UISegmentedControl

簡單的分段控制器 實現(xiàn)在導(dǎo)航條上

導(dǎo)入新建控制器
#import "ViewController.h"   
#import "Controller1.h"
#import "Controller2.h"

@interface ViewController (){
    Controller1 *V1;
    Controller2 *V2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//設(shè)置控件幾個
NSArray*array = [NSArray arrayWithObjects:@"Controller1",@"Controller2",nil];
UISegmentedControl *segC = [[UISegmentedControl alloc]initWithItems:array];
 //開始時默認(rèn)選中下標(biāo)(第一個下標(biāo)默認(rèn)是0)
 segC.selectedSegmentIndex = 0;
    //添加分段控制器的點擊事件
    [segC addTarget:self action:@selector(add:) forControlEvents:UIControlEventValueChanged];
  //控件渲染色(也就是外觀字體顏色)
   segC.tintColor= [UIColor blackColor];
//按下是否會自動釋放:
self.navigationItem.titleView = segC;
  V1 = [[Controller1 alloc]init];
    V2 = [[Controller2 alloc]init];
  //設(shè)置點進去第一個控制器
  [self.view addSubview:V1.view];
}
//switch判斷點進去的控制器
- (void)add:(UISegmentedControl *)segmt{
    int index = (int)segmt.selectedSegmentIndex;
    switch(index) {
    case 0:{
            [self.view addSubview:V1.view];
            [V2.view removeFromSuperview];
        }
            break;
        case 1:{
            [self.view addSubview:V2.view];
            [V1.view removeFromSuperview];
        }
            break;
        default:
            break;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容