UISegmentedControl 使用

#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];


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。