一、RootView 跳到SecondViewgit
首先咱們須要新一個View。新建SecondView,按住Command鍵而後按N,彈出新建頁面,咱們新建SecondViewgithub
二、爲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