一、RootView 跳到SecondViewgit
首先咱们须要新一个View。新建SecondView,按住Command键而后按N,弹出新建页面,咱们新建SecondView
github
二、为Button 添加点击事件,实现跳转spa
在RootViewController.xib中和RootViewController.h文件创建链接.net

在RootViewController.m中实现代码,alloc一个SecondViewController,用pushViewController到navigationController中去,并为orm
SecondViewController这是title为 secondView.title =@"Second View"; 默认状况下,titie为下个页面返回按钮的名字。blog
- - (IBAction)gotoSecondView:(id)sender {
- SecondViewController *secondView = [[SecondViewController alloc] init];
- [self.navigationController pushViewController:secondView animated:YES];
- secondView.title = @"Second View";
- }
这是点击GotoSecondView 按钮,出现

这就是SecondView了。事件
三、添加segmentedControllerget
在nav bar这样的效果是如何实现的呢?it

这就是segmentedController。io
3.1在RootViewController.m的viewDidLoad添加以下代码:
- NSArray *array = [NSArray arrayWithObjects:@"鸡翅",@"排骨", nil];
- UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];
- segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;
-
- [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
- self.navigationItem.titleView = segmentedController;
3.2[segmentedController addTarget:selfaction:的实现
- -(void)segmentAction:(id)sender
- {
- switch ([sender selectedSegmentIndex]) {
- case 0:
- {
- UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了鸡翅" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
- [alter show];
-
- }
- break;
- case 1:
- {
- UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了排骨" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
- [alter show];
- }
- break;
-
- default:
- break;
- }
- }
这样就能响应鸡翅和排骨按钮了
四、自定义backBarButtonItem
左上角的返回上级View的barButtonitem的名字是上级目录的Title,若是title或者适合作button的名字,怎么办呢?咱们能够本身定义
代码以下:
- UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根视图" style:UIBarButtonItemStyleDone target:nil action:nil];
- self.navigationItem.backBarBu
效果:

六、自定义title
UINavigationController的title能够用别view替代,好比用UIButton UILable等,下面我用UIButton.
在SecondViewController.m中添加下面以下。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- [button setTitle: @"自定义title" forState: UIControlStateNormal];
- [button sizeToFit];
- self.navigationItem.titleView = button;}
运行程序,goto secondView,运行效果

例子代码:https://github.com/schelling/YcDemo