iOS學習筆記——多控制器管理

NavigationController

在StoryBoard中添加NavigationController 數組

在上網看到不少都是用xib添加,使用StoryBard的有兩種辦法,但我以爲下面用到那種方式比較好,直接在StoryBard中拖入一個NavigationController,在StoryBard中出現兩個視圖,一個是NavigationController,另外一個是TableViewController。app

TableViewController不是必要存在的,只是默認添加上去的,加入須要其餘視圖,則能夠把它刪除,從新拖入須要的視圖,在NavigationController中按Ctrl鍵拖到新的視圖中,這時彈出的Segue會比日常多了一項,它是屬於RelationShip Segue分組的root view controller。工具

使用該Segue後,會發現第二個視圖中出現了導航欄。把示意app啓動引導的那個箭頭拉到第一個視圖,也就是NavigationController前面,flex

啓動app會發現,app啓動並不是引導到NavigationController,而是後來新加的那個視圖。在啓動後打開的那個頁面的導航欄中修改Title屬性爲"首頁",那麼日後咱們也稱這個頁面爲"首頁",再往StoryBoard上面拖一個新的視圖,在"首頁"中添加一個Button按鈕,給Button和新的頁面創建Segue,類型選擇爲push,這個類型與往經常使用的Modal不同,使用後本來空白的頁面上多了導航欄和工具欄,而後在這個頁面的導航欄中修改Title屬性爲"次頁",保存運行一下,當點擊Button跳轉到次頁的時候,發現導航欄多了一個後退的按鈕,按鈕上的文字也寫明是"首頁",故名思意就知道點擊該按鈕就能返回到"首頁"了spa

 

 

NavigationItem添加 3d

上面提到的後退按鈕,實際上是導航欄上面的一種導航欄元素,對於一個導航欄它會有LeftBarItem、RightBarItem、TitleView還有一個比較特殊的位於導航欄左邊的BackBarItem。經本人嘗試,若是一個導航欄上有BackBarItem和LeftBarItem,那麼BackBarItem會被LeftBarItem覆蓋,但這個有多是本人嘗試的範圍有點窄,涵蓋不了全部狀況,按本人推斷有多是最後一個加到導航欄的就顯示到導航欄上。那下面就演示一下單純在導航欄上添加NavagationItem。code

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];

self.navigationItem.leftBarButtonItem = leftButton;

 

 

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)];

self.navigationItem.rightBarButtonItem = rightButton;

在上述代碼中添加了左部按鈕和右部按鈕,兩個按鈕都被綁定了點擊事件,調用不一樣的方法,若是點擊後無操做則傳入nil則可。效果以下blog

兩個按鈕的圖標有所差異,這個在初始化方法的第一個參數所控制,其餘值和圖片分別以下表所示索引

  • UIBarButtonSystemItemAdd
  • UIBarButtonSystemItemCompose
  • UIBarButtonSystemItemReply
  • UIBarButtonSystemItemAction
  • UIBarButtonSystemItemOrganize
  • UIBarButtonSystemItemBookmarks
  • UIBarButtonSystemItemSearch
  • UIBarButtonSystemItemRefresh
  • UIBarButtonSystemItemStop
  • UIBarButtonSystemItemCamera
  • UIBarButtonSystemItemTrash
  • UIBarButtonSystemItemPlay
  • UIBarButtonSystemItemPause
  • UIBarButtonSystemItemRewind
  • UIBarButtonSystemItemFastForward
  • UIBarButtonSystemItemUndo
  • UIBarButtonSystemItemRedo

加入TitleView的代碼以下所示three

NSArray *array = [NSArray arrayWithObjects:@"元素1",@"元素2", nil];

UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];

segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;

 

[segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

self.navigationItem.titleView = segmentedController;

 

 

這樣程序運行起來就有一個分段控件在標題處

由於返回按鈕是選擇了push類型的Segue而自動生成的,而按鈕上的文字都是自動添加上去的,加入要給這個按鈕進行修改設置,單純構造一個UIBarButtonItem賦值到self.navigationItem.backBarButtonItem屬性上是不行的。由於這個self.navigationItem.backBarButtonItem並不是本頁面的返回按鈕,實際是以本頁面爲起始頁而導航到的下一頁的返回按鈕。引用一個網友的說法就是好比在AController跳轉到BController,在A設置了self.navigationItem.backBarButtonItem,這個backBarButtonItem爲BController的self.navigationController.navigationBar.backItem.backBarButtonItem。那麼在要設置這個返回按鈕則須要在HGNaviView1Controller中添加如下代碼

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根視圖" style:UIBarButtonItemStyleDone target:self action:@selector(On_BackButton_Click)];

 

[self.navigationItem setBackBarButtonItem:backButton];

 

效果則以下圖

在NavicationController中有兩個屬性是設置Bar Visibility,一個是設置導航欄的可視性;另外一個設置工具欄的可視性。

一樣也能夠經過代碼設置

[self.navigationController setToolbarHidden:false animated:true];

 

經過上面兩種方式設置的結果都是全局的,凡是跟同一個NavigationController創建Segue的視圖都會受影響。往工具欄上面添加按鈕相似在導航欄上添加按鈕,都是使用UIBarButtonItem,但添加到工具欄時是經過一個數組,代碼以下所示

UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];

UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];

UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];

UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];

UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

 

[self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];

 

固然ToolBar能夠自行添加,但若是不把NavigationController統一創建的工具欄隱藏掉的話會致使視圖上會有兩個工具欄,假如只是個別頁面須要顯示工具欄的話我的仍是以爲把NavigationController統一創建的工具欄隱藏,在須要的界面中自行添加工具欄

[self.navigationController setToolbarHidden:true animated:true];

UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];

UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];

UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];

UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];

UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

 

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - 44.0, self.view.frame.size.width, 44.0)];

[toolBar setBarStyle:UIBarStyleDefault];

toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

[toolBar setItems:[NSArray arrayWithObjects: flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem,nil]];

[self.view addSubview:toolBar];

 

效果和上圖徹底同樣

TabBarController

TabBarController使用

在使用過NavigationController以後使用TabBarController就容易上手了,在StoryBoard中拖入TabBarController,狀況與NavigationController的相似,第一個頁面TabBarController並不是會呈現到用戶界面中去,它只是起到一個管理各個子頁面的效果,每一個選項卡的具體內容會在他們的視圖中編輯,這裏默認會創建兩個Tab,

若是須要再多的Tab,就能夠直接拖入對應的ViewController,在TabBarController頁面按Ctrl鏈接到新頁面創建Segue,這裏的Segue類型又有不一樣,Relationship Segue分組中有一個view controllers,就選擇那個能夠了,這樣在TabBarController中會多加了一個Tab。

Tab修改

每一個Tab的圖標和文字均可以在相應的Tab視圖中修改,

圖標的尺寸是有限制的,在30*30,若是過大了就會有部分顯示不出來,並且就算是給的圖標是一個彩色的圖片,最終顯示在界面上的都只剩下一個輪廓,原圖與顯示在TabBar中的對比

在代碼中一樣能夠用相應的屬性對Tab的標題和圖片進行設置

[self.tabBarItem setImage:[UIImage imageNamed:@"SunFlower.gif"]];

[self.tabBarItem setTitle:@"第一選項卡"];

 

不過有另外一個方法setFinishedSelectedImage:withFinishedUnselectedImage則能夠把圖標完整顯示出來,若是圖標過大則會佔用下面文字的空間,並且圖標會按本來的顏色顯示出來,圖標的顯示還會按照選項卡的選中狀態來決定

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"SunFlower.gif"] withFinishedUnselectedImage:[UIImage imageNamed:@"Ixia.gif"]];

 

選中時

未選中時

在每一個TabBarItem中都有一個badgeValue屬性,用於設置Tab的小紅圈的值,他是NSString,因此能夠給他賦的值就不侷限在數字,還但是其餘字符

self.tabBarItem.badgeValue=@"new";

 

Tab的數量能夠無限地增長,可是在節目是最多能夠顯示五個Tab標籤,若是Tab的數量多於5個的時候,本來第5個的Tab就會變成,而且點擊進去是一個帶TableView的導航頁,從第5個開始的Tab會在TableView中顯示,從TableView中導航到的Tab頁都會帶導航欄,以返回TabeView那一頁

導航欄上面還有編輯按鈕進入配置Tab的頁面

在這一頁中能夠拖拽各個Tab標籤到底部的TabBar中以更改Tab在TabBar中的順序,而這裏顯示的Tab是能夠經過UITabBarController的customizableViewControllers屬性設定的,默認狀況下它是和UITabBarController的viewController屬性(這個就是UITabBarController擁有Tab的集合)擁有相同的元素,可是也能夠經過設置把第6個Tab從customizableViewControllers中去除

NSArray *viewControllers= self.tabBarController.viewControllers;

NSMutableArray *newViewControllers=[NSMutableArray arrayWithCapacity:viewControllers.count-1];

for (int i=0; i<[viewControllers count]-1; i++) {

    [newViewControllers addObject:[viewControllers objectAtIndex:i]];

}

self.tabBarController.customizableViewControllers=newViewControllers;

 

結果第6個Tab就不會出如今Configure頁面中了

而對於viewControllers有的Tab而customizableViewControllers沒有的Tab,則該部分Tab在編輯狀態下不會被移除出TabBar。

關於選中的Tab

經過selectedIndex和selectedViewController均可以獲取或設置被選中的Tab,對於設置selectedIndex和selectedViewController而言,僅僅能設置能夠選中的Tab,並不能把頁面切換過去,並且對於More這個Tab來講還有點特別的狀況

  • 選中了More,selectedIndex是不能獲取其準確的索引值,而selectedViewController能夠獲取到它的UITabBarItem;
  • 若是對selectedIndex賦上大於等於5的值,Tab就會選中More;
  • 若是對selectedViewController賦上tabBarController.moreNavigationController或者索引值大於或等於5的UITatBarItem,Tab就會選中More

UITabBarController的tabBar屬性裏面也能夠經過selectedItem獲取選中的UITabBarItem,可是並不能給這個selectedItem賦值,這回引起異常。雖然UITabBar有一些方法是能夠改變自身狀態,但不能給UITabBarController的tabBar修改自身狀態。

UITabBarController的事件

UITabBarController擁有一些事件,經過這些事件能夠監測Tab發生變化,這須要給ViewController實現UITabBarControllerDelegate協議,而後設置tabBarController的delegate屬性

self.tabBarController.delegate=self;

 

事件歸爲兩類,一類是單純tabBar上Tab的選中狀態發生變動先後出發的

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

 

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

 

 

先觸發前者再觸發後者,前者有一個布爾類型的返回值,若是返回的是False則會取消Tab的切換,可是經過代碼設置SelectedIndex除外。兩個參數tabBarController表明着起始的TabviewController表明着目標的Tab

另外一類是針對moreViewControlleredit操做的

-(void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers

 

-(void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed

 

-(void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed 

WillBegin…是在進入Edit界面時觸發的,WillEnd…didEnd….是在點擊了Done以後相繼觸發的。

相關文章
相關標籤/搜索