UINavigationController相關,uinavigationbar

來源:互聯網
上載者:User

UINavigationController相關,uinavigationbar

掌握:

  1. UINavigationController的使用:添加、移除控制器。

  2. UINavigationBar內容的設定。

 ---------------------------------------------------------------------------------------------------------

 一、控制器的添加和移除:

  1. UINavigationController以棧的形式儲存子控制器:

    @property(nonatomic,copy) NSArray *viewControllers;

    @property(nonatomic,readonly) NSArray *childViewControllers;

 

2. 使用push方法能將某個控制器壓入棧

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

 

 /** 下面方法中用作示意*/

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          self.window.backgroundColor = [UIColor whiteColor];
    
      // 1.建立導航控制器
            XZOneViewController *one = [[XZOneViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:one]; // 傳入一個棧底控制器來初始化導航控制器 (常用)

  
      //  拿到棧頂控制器(顯示在眼前的控制器)
      //  nav.topViewController


      //  存放所有子控制器的棧
      //  nav.viewControllers
      //  這個數組也存放子控制器
      //  nav.childViewControllers


      //  2.添加子控制器
      //  XZOneViewController *one = [[XZOneViewController alloc] init];
      //  [nav addChildViewController:one];                // 這樣子也能把one控制器放到 數組 viewControllers 和  childViewControllers 中。
      //  [nav pushViewController:one animated:YES]; // 將one壓入棧中,即放入 viewControllers 和 childViewControllers 中。 (推薦用法,有動畫)

      //  nav.viewControllers = @[one];  // 這樣也是設定
      //  nav.viewControllers = @[one];  // 不能這麼幹,因為viewControllers是唯讀。


      // 3.設定為視窗的根控制器
          self.window.rootViewController = nav;
    
          [self.window makeKeyAndVisible];
          return YES;
}

 

3. 使用pop方法可以移除控制器  // 將棧頂的控制器移除

  - (UIViewController *)popViewControllerAnimated:(BOOL)animated;

 

  // 回到指定的子控制器

  - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

 

  // 回到根控制器(棧底控制器)

  - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

   4. 導覽列的內容由棧頂控制器的navigationItem屬性決定     UINavigationItem有以下屬性影響著導覽列的內容    // 左上方的返回按鈕

      @property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;

     // 中間的標題視圖

      @property(nonatomic,retain) UIView          *titleView;

        // 中間的標題文字

      @property(nonatomic,copy)   NSString        *title;

          // 左上方的視圖

      @property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;

 

    // 右上方的視圖

      @property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;

 

二、控制器的view結構 以及 UINavigationBar導航條內容的設定:

  1. 情景一:

      self.navigationItem.title = @"第一個控制器";
   
      UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];   
      UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];
    
      self.navigationItem.rightBarButtonItems = @[item1, item2];
      self.navigationItem.leftBarButtonItems   = @[item1, item2];

  

  2. 情景二:

      self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
      self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.