iOS開發UI篇—導航控制器屬性和基本使用 ios
1、導航控制器的一些屬性和基本使用數組
1.把子控制器添加到導航控制器中的四種方法app
(1)atom
1.建立一個導航控制器spa
UINavigationController *nav=[[UINavigationControlleralloc]init];代理
2.設置導航控制器爲window的根視圖code
self.window.rootViewController=nav;orm
3.添加xml
YYOneViewController *one = [[YYOneViewController alloc] init];blog
[nav pushViewController:one animated:YES];
(2)
1.建立一個導航控制器
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設置導航控制器爲window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
[nav addChildViewController:one];
(3)
1.建立一個導航控制器
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設置導航控制器爲window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
nav.viewControllers=@[one];(添加到導航控制器的棧中)
說明:nav.viewControllers;== nav.childViewControllers;注意該屬性是隻讀的,所以不能像下面這樣寫。nav.childViewControllers = @[one];
(4)最經常使用的方法
YYOneViewController *one=[[YYOneViewController alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];
2.當前子控制器界面導航欄的標題以及對應返回標題的設置
self.navigationItem.title=@"第一個界面";
self.navigationItem.backBarButtonItem=[[UIBarButtonItemalloc]initWithTitle:@"返回一" style:UIBarButtonItemStylePlaintarget:nilaction:nil];
3.給導航欄添加按鈕
說明:可添加一個,也能夠添加多個(數組)
添加導航欄左邊的按鈕(添加一個相機圖標的按鈕),會蓋掉返回
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
4.界面跳轉
跳轉到第二個界面(當前爲第三個,移除當前棧頂的控制器) [self.navigationControllerpopViewControllerAnimated:YES];
移除處理棧底控制器以外的全部控制器 [self.navigationControllerpopToRootViewControllerAnimated:YES];
只要傳入棧中的某一個控制器,就會跳轉到指定控制器 [self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];
2、代碼示例
YYAppDelegate.m文件
YYOneViewController.m文件
YYTwoViewController.m文件
//
// YYTwoViewController.m
// 01-導航控制器的使用1
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYTwoViewController.h"
#import "YYThreeViewController.h"
@interface YYTwoViewController ()
- (IBAction)jump2Three:(id)sender;
@end
@implementation YYTwoViewController
//跳轉到第三個子控制器
- (IBAction)jump2Three:(id)sender {
//1.建立第三個子控制器
YYThreeViewController *three=[[YYThreeViewController alloc]init];
//2.將子控制器添加到導航控制器中
[self.navigationController pushViewController:three animated:YES];
}
-(void)viewDidLoad
{
[super viewDidLoad];
//給導航欄添加按鈕
//添加導航欄左邊的按鈕(添加一個相機圖標的按鈕),會蓋掉返回
// self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
//爲導航欄在右邊添加多個按鈕
//建立兩個按鈕
UIBarButtonItem *a=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:nil action:nil];
UIBarButtonItem *b=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:nil action:nil];
UIBarButtonItem *c=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
self.navigationItem.rightBarButtonItems=@[a,b,c];
//設置對應的導航條的返回(第三個界面導航條的返回)
self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];
}
@end
YYThreeViewController.m文件
//
// YYThreeViewController.m
// 01-導航控制器的使用1
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYThreeViewController.h"
#import "YYTwoViewController.h"
@interface YYThreeViewController ()
//返回到第二個控制器頁面
- (IBAction)jump2two:(id)sender;
//返回到第一個控制器頁面
- (IBAction)jump2root:(id)sender;
@end
@implementation YYThreeViewController
- (IBAction)jump2two:(id)sender {
//跳轉到第二個界面(移除當前棧頂的控制器)
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)jump2root:(id)sender {
//移除處理棧底控制器以外的全部控制器
[self.navigationController popToRootViewControllerAnimated:YES];
// 只要傳入棧中的某一個控制器,就會跳轉到指定控制器
//不能這樣,沒添加到導航控制器YYTwoViewController *two = [[YYTwoViewController alloc] init];
//[self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];
}
@end
實現效果:
3、導航控制器經過棧來管理子控制器
示意圖
:
說明:
導航控制器是經過棧的形式來管理子控制器的(先進後出)
顯示在導航控制器上得view永遠是棧頂控制器的view
一個導航控制器只有一個導航條,也就是說全部的自控制器公用一個導航條。
4、補充
在代理方法中,打印當前window下面的全部子控件,並經過xml文件來保存,代碼以下。
注意:在ios7和之前版本中,各個控件,包括子控制器界面frame的不一樣。