1. 程式人生 > >iOS---分段控制元件的點選事件

iOS---分段控制元件的點選事件

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

//先建立一個數組用於設定標題

UISegmentedControl *segment = [[UISegmentedControlalloc]initWithItems:arr];

            segment.width=self.view.width;

// segment.height=cell.height;

           segment.selectedSegmentIndex =0;//設定預設選擇項索引

            segment.tintColor

= [UIColor orangeColor];

            [segment addTarget: self action: @selector(selected:)  forControlEvents:UIControlEventValueChanged

             ];

            segment.backgroundColor=[UIColor whiteColor];

            //取消選中的背景色效果

// segment.momentary = YES;

            [cell addSubview:segment];

#pragma mark - 分段選擇的點選事件

-(void)selected:(id)sender{

UISegmentedControl* control = (UISegmentedControl*)sender;

switch (control.selectedSegmentIndex) {

        case 0:

            //

              NSLog(@" 0 ");

            break;

        case 1:

            //

             NSLog(@" 1 "

);

            break;

        case 2:

            //

             NSLog(@" 2 ");

            break;

        default:

            break;

    }

}