iOS個人整理13-導航控制器-UINavigationController

一、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];
}


實現了效果也比較簡單,點擊首頁的“前往第二頁”可以跳到第二頁

點擊第二頁的“首頁”或者“回到首頁”可以調回首頁,

其中“首頁”按鈕是系統自動加的,“回到首頁”是我們自己寫的

下一節要專門總結一下界面跳轉的同時的傳值問題



  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章