天天看點

iOS學習總結之導航控制器

一、初始化

  ViewController *viewController = [[ViewController alloc]init];

//    導航控制器初始化  設定導航控制器的第一個頁面

    UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:viewController];

    self.window.rootViewController = nv;

二、MVC(設計方式)Model(模型 資料 或者 邏輯運算 操作)- View(視圖)-Controller(視圖控制器)

所有的iOS應用程式基本都使用 Model-View-Controller 或稱 MVC 架構。從架構角度來看 Model、View和Controller是一個iOS應用程式的 三個 重要元件 。

三、通過點選按鈕調到指定頁面需要先将跳轉頁面初始化。

    TwoViewController *two = [[TwoViewController alloc]init];

//    通過導航控制器跳到下一個頁面

    [self.navigationController pushViewController:two animated:YES];

四、跳轉頁面

1、當跳轉到頁面時,如果沒有設定背景顔色,就會出現push卡頓現象,是以需要先設定背景顔色。

    self.view.backgroundColor = [UIColor purpleColor];

2、設定導航欄的标題

    self.title = @“登入";

3、修改控制欄上按鈕的屬性

1>擷取控制欄上的左邊按鈕

UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(back)];

2>自定義導航欄導航文字

    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithTitle:@"傳回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];

    self.navigationItem.leftBarButtonItem = back;

3>在傳回方法中實作如下方法即可放回上一頁面

 [self.navigationController popViewControllerAnimated:YES];

4>在導航欄右邊加一張按鈕圖檔

   1)先初始化設定一個按鈕

   2)添加至導航控制器中

方法一:

  [self.navigationController.navigationBar addSubview:按鈕];

方法二:

    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:right];

    self.navigationItem.rightBarButtonItem = item;

5>傳回首頁方法

[self.navigationController popToRootViewControllerAnimated:YES];

6>傳回指定頁面方法

//  viewControllers 記錄了 所有push壓棧過的viewControllers

//    在不想傳回上一個頁面 也不想傳回首頁的時候

    [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];

繼續閱讀