提供多個按鈕

功能:
UISegment 提供了一個可按鈕的控制項

實作:

[xcode]
– (IBAction)segmentedAction:(id)sender {
switch ([seg selectedSegmentIndex]) {
case 0:
NSLog(@"%d", [seg selectedSegmentIndex] );
break;
case 1:
NSLog(@"第二個" );
break;
case 2:
NSLog(@"第三個" );
break;
default:
break;
}
}
[/xcode]

參考一:

實作二:
[xcode]
NSArray *buttons = [NSArray arrayWithObjects:@"選擇一",@"選擇二",nil];
UISegmentedControl * segSelect = [[UISegmentedControl alloc] initWithItems:buttons];
[segSelect addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
[segSelect setFrame:CGRectMake(80,10,160,60)];
[segSelect setSelectedSegmentIndex:0];
[segSelect insertSegmentWithTitle:@"選擇三" atIndex:2 animated:YES];
[self.view addSubview:segSelect];
[/xcode]

實作二行為:
[xcode]
-(void)segmentedAction:(id)sender{
NSLog(@"選擇%d", [seg selectedSegmentIndex] );
}
[/xcode]

參考二:

屬性 property :
initWithItems 設定初始化的項目(數量)
segmentedControlStyle 樣式
[c]
typedef enum {
UISegmentedControlStylePlain,
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,
UISegmentedControlStyleBezeled,
} UISegmentedControlStyle;
[/c]

方法 Method:
– insertSegmentWithImage:atIndex:animated: 增加圖片按鈕
– insertSegmentWithTitle:atIndex:animated: 增加文字按鈕

廣告