UITabBarController 和 UINavigationController 的詳解

來源:互聯網
上載者:User

標籤:

首先得搞清這兩個控制器之間的層級關係,我們直接看官網給的圖,如下所示:

從這張圖可以看到:最右邊的Assembled views是呈現給使用者的介面,它左邊的Window是最底層的視窗,重點來了,再往左,是Tab bar view,Tab bar view的上方是Navigation view,最後是使用者定製的視圖。

看完這個,代碼就應該很好寫了,我們需要把Navigation view加到 Tab bar view的內容上去,Tab bar view再加到Window上去。就是Window套UITabBarController,UITabBarController套UINavigationController, UINavigationController套UIViewController。

UIViewControlleller  的tabBarItem的詳情裡面最後一句話: 

The default value is a tab bar item that displays the view controller‘€™s title.

標籤欄元素預設顯示的是 視圖控制器的標題。

navigationItem 裡面同樣有這麼一句話

The default behavior is to create a navigation item that displays the view controller‘€™s title.

預設建立一個導覽列元素顯示 視圖控制器的標題

1.因此 activetyLC.title = @"活動"; 同時給導覽列和標籤欄賦值  就有跡可循了。

2. 若 我們的導覽列標題 和標籤欄 標題顯示的不一樣的時候如何賦值呢。 


UIViewController是UINavigationController和 UITabBarController的父類

我們看下UIViewController裡面的navigationItem 和 tabBarItem 是如何說明的

 

The tab bar item that represents the view controller when added to a tab bar controller.

標籤欄元素 代表 控制器被  添加到的標籤欄控制器 的標籤欄元素 (父標籤欄 標籤元素)

The navigation item used to represent the view controller in a parent‘€™s navigation bar. (read-only)

導覽列元素代表著 父視圖控制器的導覽列 元素 (父 導覽列 導航元素)

 

按照上面我所說的層次關係,在 最上層的 UIViewController

這樣寫:

 self.navigationItem.title = @"活動";

 

  self.tabBarItem.title = @"活動123";  會如何??

結果是: 導覽列 標題 顯示  “活動” ,標籤欄為 “空”。

若我們給導覽列的 tabBarItem 賦值。

activetyNC.tabBarItem.title = @"活動";

則 標籤欄 正常顯示 “活動”。 

結合上面的 UIViewController裡面對 導航元素和 標籤元素的 介紹。 這樣就明白了。

雖然 


UINavigationController和 UITabBarController 都繼承自 UIViewController,他們中都有 標籤元素和導航元素屬性,但是

對於:UIViewController navigationItem 和 tabBarItem 沒有賦值,則為空白,若父視圖為導覽列控制器,navigationItem為 導覽列控制器的 navigationItem; 若父類為 標籤控制器,則 tabBarItem為 標籤控制器的 tabBarItem。

 

UITabBarController 和 UINavigationController 的詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.