UISegmentedControl

初始化

初始化的數組中 必須是 字符串 或者 圖片
如果選用圖片初始化 必須使用鏤空圖

NSArray *itemArray = @[@"第一段",@"第二段",@"第三段"];

分段按鈕

UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:itemArray];

設置位置

segment.frame = CGRectMake(20, 100, 300, 50);

設置背景顏色

segment.backgroundColor = [UIColor cyanColor];

設置默認選中(從0開始)

segment.selectedSegmentIndex = 1;

修改選中顏色

segment.tintColor = [UIColor yellowColor];

修改中間段的寬度

[segment setWidth:200 forSegmentAtIndex:1];

添加到父視圖上

[self.view addSubview:segment];

釋放

[segment release]

創建控制器

注意 : 這裏先用屬性聲明瞭 所有直接這樣寫
屬性的聲明

把控制器 改成屬性 變量調用
@property (nonatomic ,retain)SecondViewController *secondVC;
@property (nonatomic ,retain)ThirdViewController *thirdVC;
@property (nonatomic ,retain)FourViewController *fourVC;
self.secondVC = [[SecondViewController alloc]init];

添加子控制器 並且顯示視圖

[self addChildViewController:self.secondVC];
[self.view addSubview:self.secondVC.view];

釋放

[_secondVC release];

初始化一個圖片

注意這裏的圖片是鏤空圖 而且還是PNG格式 如果不是PNG格式
則要把什麼格式加上例如

@"01-refresh.jpg"
UIImage *image1 = [UIImage imageNamed:@"01-refresh"];

添加一個點擊事件

選取ValueChanged 實際上是監測的 索引的變化

重點方法 我們監測的是值的變化 所以我們要注意UIControlEventValueChanged

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

最前面的應該是segment1 然後是默認選中第一界面視圖 secondVC.view

[self.view bringSubviewToFront:self.secondVC.view];
    [self.view bringSubviewToFront:segment1];

實現點擊方法

- (void)segmentAction:(UISegmentedControl *)segment
{
    需求  判斷出點擊了那個分段按鈕
    NSLog(@"%ld",segment.selectedSegmentIndex);
    每個分段按鈕 控制一個界面 每個界面是一個控制器來控制 並且可以切換界面
    通過索引切換不同的頁面
    switch (segment.selectedSegmentIndex) {
        case 0:
            [self.view bringSubviewToFront:self.secondVC.view];
            break;
        case 1:
            [self.view bringSubviewToFront:self.thirdVC.view];
            break;
        case 2:
            [self.view bringSubviewToFront:self.fourVC.view];
            break;

        default:
            break;
    }
    每次都把segment放到最上面
    [self.view bringSubviewToFront:segment];
 }
UI學習第六天
發佈了32 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章