美文网首页
UISegmentedControl 使用

UISegmentedControl 使用

作者: 失忆的程序员 | 来源:发表于2018-02-06 11:58 被阅读27次

#import "OneViewController.h"

#import "TwoViewController.h"

{

    OneViewController *_oneVC;

    TwoViewController *_twoVC;

}

UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"直播",@"音乐"]];

    segment.tintColor = [UIColor whiteColor];

    segment.selectedSegmentIndex = 0;

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

    self.navigationItem.titleView = segment;

    //创建控制器的对象

    _oneVC = [[OneViewController alloc] init];

    _oneVC.view.backgroundColor = [UIColor redColor];

    _twoVC = [[TwoViewController alloc] init];

    _twoVC.view.backgroundColor = [UIColor grayColor];

    [self.view addSubview:_oneVC.view];

-(void)segmentClick:(UISegmentedControl *)segment{

    switch (segment.selectedSegmentIndex) {

        case 0:

            //第一个界面

            [self.view addSubview:_oneVC.view];

            [_twoVC.view removeFromSuperview];

            break;

        case 1:

            [self.view addSubview:_twoVC.view];

            [_oneVC.view removeFromSuperview];

            break;

        default:

            break;

    }

------------------------------------------------------------------------------------

注意 重点

// 注意要加进去 下一级会没有导航 push不到下一页

    [self addChildViewController:_oneVC];

    [self addChildViewController:_twoVC];

相关文章

网友评论

      本文标题:UISegmentedControl 使用

      本文链接:https://www.haomeiwen.com/subject/fxhgzxtx.html