一、UINavigationController基本屬性
寫這麼多文章居然沒人給我評論幾句。。。
UINavigationController,導航控制器,用來管理多個視圖控制器
它就是管理視圖控制器的控制器
從此可以在Appdelegate.m的launch函數中先聲明導航欄控制器,再将它作為視圖控制器的爸爸,window的根視圖控制器,像下面這樣
//初始化一個表視圖控制器
RootTableViewController *rootTableVC = [[RootTableViewController alloc]init];
//初始化一個導航控制器,把表視圖控制器加上去
UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootTableVC];
//設定導航控制器為window的根視圖控制器
self.window.rootViewController = navigationC;
導航欄有個重要的屬性,它的不透明效果,它會影響坐标原點的位置
self.navigationController.navigationBar.translucent
當開啟時螢幕左上角為原點
當關閉時,導航欄的左下角為原點
iOS7以後預設為YES
下面說導航控制器的各種屬性
//初始化一個視圖控制器
RootViewController *rootVC = [[RootViewController alloc]init];
//初始化導航欄視圖控制器,并加載一個根視圖控制器
UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];
//設定導航欄視圖控制器為根視圖控制器
self.window.rootViewController = navigationC;
//标題
self.navigationItem.title = @"block首頁";
//設定背景色
self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
//标題顔色
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
//導航欄顔色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];
//導航欄元素顔色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//也可以設定中間的标題為視圖
UISegmentedControl *qqSegmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"消息",@"電話"]];
self.navigationItem.titleView = qqSegmentedControl;
//添加左側按鈕,使用系統的按鈕樣式
UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
self.navigationItem.leftBarButtonItem = leftBarBtn;
//添加右側按鈕使用定義文字的初始化方式
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"去第二頁" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
self.navigationItem.rightBarButtonItem = rightBarBtn;
//還可以使用圖檔給或者View給bar按鈕初始化
//UIBarButtonItem *test1 = [UIBarButtonItem alloc]initWithImage:<#(nullable UIImage *)#> style:<#(UIBarButtonItemStyle)#> target:<#(nullable id)#> action:<#(nullable SEL)#>
//UIBarButtonItem *test2 = [UIBarButtonItem alloc]initWithCustomView:<#(nonnull UIView *)#>
//可以給右側或者左側添加按鈕組,把按鈕放入數組
//self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:test,test1, nil];
系統的導航欄按鈕樣式有很多,下圖很不錯,來自友人
二、UINavigationController控制頁面跳轉
導航欄控制器的主要作用是控制頁面跳轉
現在我們建立了兩個視圖控制器:
RootViewController
SecondViewController
1.在AppDelegate.m中建立RootViewController對象和導航控制器
//建立根視圖
RootViewController *rootVC = [[RootViewController alloc]init];
//建立導航控制器
UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];
//添加到window根視圖控制器
self.window.rootViewController = navigationC;
2.在RootViewController.m中給NavigationController添加按鈕,給按鈕添加跳轉方法并實作
- (void)viewDidLoad {
[super viewDidLoad];
//建立右側按鈕
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"前往第二頁" style:UIBarButtonItemStylePlain target:self action:@selector(jumpToSecondVC)];
//添加按鈕到導航欄
self.navigationItem.rightBarButtonItem = rightBarBtn;
}
-(void)jumpToSecondVC
{
//建立secondVC,要導入SecondViewController的.h檔案
SecondViewController *secondVC = [[SecondViewController alloc]init];
//将secondVC入棧,棧頂的視圖為正在顯示的視圖
[self.navigationController pushViewController:secondVC animated:YES];
}
3.在SecondViewController.m中添加一個傳回按鈕,點選可以傳回rootVC
系統會預設在左上角添加一個傳回按鈕,我們可以重新添加
- (void)viewDidLoad {
[super viewDidLoad];
//建立右側按鈕
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"回到首頁" style:UIBarButtonItemStylePlain target:self action:@selector(jumpToRootVC)];
//添加按鈕到導航欄
self.navigationItem.rightBarButtonItem = rightBarBtn;
// Do any additional setup after loading the view.
}
-(void)jumpToRootVC
{
//将目前的視圖控制器出棧
[self.navigationController popViewControllerAnimated:YES];
//也可直接傳回根視圖控制器
[self.navigationController popToRootViewControllerAnimated:YES];
//或者傳回指定層的視圖控制器
//可以得到目前棧内的視圖,存在數組 self.navigationController.viewControllers裡
[self.navigationController popToViewController: self.navigationController.viewControllers[0]animated:YES];
}
實作了效果也比較簡單,點選首頁的“前往第二頁”可以跳到第二頁
點選第二頁的“首頁”或者“回到首頁”可以調回首頁,
其中“首頁”按鈕是系統自動加的,“回到首頁”是我們自己寫的
下一節要專門總結一下界面跳轉的同時的傳值問題