-(void) viewDidLoad{ /*************分段控件UISegmentdControl*************/ UISegmentedControl *segmentedControl = [[UISegmentedControlalloc] initWithItems:nil]; segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; //控件風格小按鈕,適合導航欄: segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled; //控件風格 segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered; //控件風格黑邊的大白按鈕,適合表格單元 segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; //控件風格灰邊大白按鈕,適合偏好設置單元 segmentedControl.tintColor = [UIColor blueColor]; //渲染色彩 //添加片斷,從0單元開始 ,可加入標題或圖片兩種狀況 [segmentedControl insertSegmentWithTitle:@"First"atIndex:0animated:NO]; [segmentedControl insertSegmentWithTitle:@"Second"atIndex:1animated:NO]; UIImage *myImage = [UIImage imageNamed:@"1.png"]; [segmentedControl insertSegmentWithImage:myImage atIndex:2animated:NO]; //刪除片斷 [segmentedControl removeSegmentAtIndex:1 animated:YES]; //刪除單個 [segmentedControl removeAllSegments]; //刪除全部 //片斷標題 [segmentedControl setTitle:@"Third" forSegmentAtIndex:2];//設置標題 NSString *myTitle = [segmentedControl titleForSegmentAtIndex:0];//讀取標題 //圖像 [segmentedControl setImage:[UIImageimageNamed:@"2.png"] forSegmentAtIndex:0];//設置圖像 UIImage *myImage = [segmentedControl imageForSegmentAtIndex:3]; //讀取圖片 //設置片斷寬度 [segmentedControl setWidth:64.0 forSegmentAtIndex:0]; //瞬時單擊 segmentedControl.momentary = YES; //按鈕被按下後很快恢復,默認爲選中狀態就一直保持 //初始化默認片斷 segmentedControl.selectedSegmentIndex = 0; //初始指定第0個選中 //顯示控件 [parentView addSubview:segmentedControl]; //添加到父視圖 self.navigationItem.titleView = segmentedControl; //添加到導航欄(經過視圖控制器) //讀取控件 int selectIndex = segmentedControl.selectedSegmentIndex; //對應當前被選總的片斷號碼 [segmentedControl addTarget:selfaction:@selector(controlPressed:) forControlEvents:UIControlEventValueChanged];
} spa
//SegmentedControl觸發的動做 code
-(void)controlPressed:(id)sender{ 圖片
UISegmentedControl *control = (UISegmentedControl *)sender; rem
if (control == mySegmentedControl) { get
int x = control.selectedSegmentIndex; it
/*添加代碼,對片斷變化作出響應*/ io
} import
} sed