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