UISegmentedControl

#define SCREEN_WIDHT [[UIScreen mainScreen] bounds].size.width

#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

#define COMMON_HEIGHT SCREEN_HEIGHT * 0.054


UISegmentedControl是這樣用滴:

//設置一下要展示的數據

    NSArray *controlArray = [[NSArray alloc]initWithObjects:@"1",@"2", nil];

    //初始化UISegmentedControl

    UISegmentedControl *control = [[UISegmentedControl alloc]initWithItems:controlArray];

    control.frame = CGRectMake(10, 74, SCREEN_WIDHT-20, COMMON_HEIGHT);

    //設置背景顏色

    control.tintColor = [UIColor grayColor];

    //設置默認選中第幾個按鈕,下標從0開始

    control.selectedSegmentIndex = 0;

    [control addTarget:self action:@selector(selector:) forControlEvents: UIControlEventValueChanged];

    [self.view addSubview:control];

-(void)selector:(id)sender{

    UISegmentedControl *control = (UISegmentedControl *)sender;

    switch (control.selectedSegmentIndex) {

        case 0:

            //這裏是第一個按鈕

            break;

        case 1:

            //這裏是第二個按鈕

            break;

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章