iOS Learning Notes-multi-controller management

Source: Internet
Author: User

Navigationcontroller

Add Navigationcontroller to Storyboard

On the internet to see a lot of is to use xib Add, using Storybard there are two ways, but I think the following way is better, directly in the Storybard dragged into a navigationcontroller, in Storybard appeared two views, One is Navigationcontroller and the other is Tableviewcontroller.

Tableviewcontroller is not necessary, just add it by default, join needs other views, you can delete it, drag it into the desired view, and press CTRL in Navigationcontroller to drag into the new view. At this point the segue will be more than usual, it belongs to the relationship Segue Group Root view controller.

After you use the segue, you will find that the navigation bar appears in the second view. Pull the arrow that shows the app to boot to the first view, which is the Navigationcontroller front,

Launching the app will reveal that the app is not booting to Navigationcontroller, but the new view that was added later. After the launch of the page to open the navigation bar to modify the title property to "Home", then we also call this page "home", and then to storyboard above a new view, in the "Home" to add a button, to the button and the new page to establish segue, Type selection for push, this type is not the same as the usual modal, the use of the original blank page on the navigation Bar and toolbar, and then in the navigation bar of this page to modify the title property is "Secondary page", save run, when the button to jump to the next page, Find the navigation bar more than a Back button, the text on the button is also stated to be "home", so the name of the idea that click on the button will be able to return to the "home"

Navigationitem add

The Back button mentioned above is actually a navigation bar element above the navigation bar, for a navigation bar it will have Leftbaritem, Rightbaritem, Titleview and a relatively special backbaritem located on the left side of the navigation bar. After I try, if a navigation bar has Backbaritem and Leftbaritem, then Backbaritem will be Leftbaritem cover, but this may be I try to narrow the range, not all situations, As I infer, it is possible that the last one added to the navigation bar will be displayed on the navigation bar. So here's a demonstration. Simply add Navagationitem on the navigation bar.

Uibarbuttonitem *leftbutton == LeftButton;   *rightbutton == Rightbutton;

In the above code to add the left button and the right button, two buttons are bound to click on the event, call different methods, if no action after the click to pass nil. The effect is as follows

The icons for the two buttons are different, this one is controlled by the first parameter of the initialization method, and the other values and pictures are shown in the following table, respectively

  • Uibarbuttonsystemitemadd
  • Uibarbuttonsystemitemcompose
  • Uibarbuttonsystemitemreply
  • Uibarbuttonsystemitemaction
  • Uibarbuttonsystemitemorganize
  • Uibarbuttonsystemitembookmarks
  • Uibarbuttonsystemitemsearch
  • Uibarbuttonsystemitemrefresh
  • Uibarbuttonsystemitemstop
  • Uibarbuttonsystemitemcamera
  • Uibarbuttonsystemitemtrash
  • Uibarbuttonsystemitemplay
  • Uibarbuttonsystemitempause
  • Uibarbuttonsystemitemrewind
  • Uibarbuttonsystemitemfastforward
  • Uibarbuttonsystemitemundo
  • Uibarbuttonsystemitemredo

The code to join Titleview is shown below

Nsarray *array = [Nsarray arraywithobjects:@ " element 1"@ " element 2"  *segmentedcontroller = = = 

This way, the program runs with a segmented control at the header.

Because the return button is automatically generated by selecting the segue of the push type, and the text on the button is automatically added, add the button to modify the settings, It is not possible to simply construct a uibarbuttonitem assignment to the Self.navigationItem.backBarButtonItem attribute. Because this self.navigationItem.backBarButtonItem is not the return button on this page, it is actually the back button of the next page that is navigated to by this page as the start page. Quote a Netizen's saying is for example in Acontroller jump to Bcontroller, In a set the Self.navigationItem.backBarButtonItem, this backbarbuttonitem for Bcontroller self.navigationController.navigationBar.backIt Em.backbarbuttonitem. Then in order to set this back button you need to add the following code in Hgnaviview1controller

Uibarbuttonitem *backbutton = [[Uibarbuttonitem alloc] Initwithtitle:@ " root View "  style: Uibarbuttonitemstyledone target:self Action: @selector (On_backbutton_click)]; [Self.navigationitem Setbackbarbuttonitem:backbutton];

The effect is as

There are two properties in Navicationcontroller that are set bar Visibility, one is to set the visibility of the navigation bar, and the other to set the visibility of the toolbar.

It can also be set by code

[Self.navigationcontroller Settoolbarhidden:false animated:true];

The results set in both ways are global, and any view that is established segue with the same navigationcontroller will be affected. Adding a button to the toolbar is like adding a button to the navigation bar, all using Uibarbuttonitem, but when added to the toolbar is an array, as shown in the code below

Uibarbuttonitem *one =*two =*three =*four =*flexitem = [[Uibarbuttonitem Alloc ] Initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace Target:nil Action:nil]; [Self Settoolbaritems:[nsarray arraywithobjects:flexitem, one, Flexitem, I, Flexitem, three, Flexitem, four, FlexItem, Nil]];

Of course toolbar can add it yourself, but if you don't hide the Navigationcontroller Unified toolbar, you'll have two toolbars on the view, If only the individual pages need to display the toolbar, the individual still feel that the Navigationcontroller unified set of toolbars hidden, in the desired interface to add their own toolbars

[Self.navigationcontroller Settoolbarhidden:trueAnimated: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, he, Flexitem, three, Flexitem, four, Flexitem,nil] ]; [Self.view Addsubview:toolbar];

The effect is exactly the same

Tabbarcontroller

Tabbarcontroller use

The use of Tabbarcontroller after the use of Navigationcontroller is easy to get started, in storyboard dragged into the Tabbarcontroller, the situation is similar to Navigationcontroller, The first page Tabbarcontroller not be rendered to the user interface, it is just a way to manage each sub-page effect, each tab's specific content will be edited in their view, where the default is to create two tab,

If you need more tab, you can drag directly into the corresponding Viewcontroller, on the Tabbarcontroller page Press CTRL to connect to the new page build segue, here the segue type is different, relationship There is a view controllers in the Segue group, just choose that, so you'll add a tab to the Tabbarcontroller.

TAB modification

Each tab's icon and text can be modified in the corresponding tab view.

The size of the icon is limited, in the 30*30, if too large there will be part of the display, and even if the icon is a color picture, the final display on the interface is only a contour, the original image and display in the Tabbar in the comparison

You can also set the tab title and picture with the corresponding properties in the code.

[Self.tabbaritem setimage:[uiimage imagenamed:@ "sunflower.gif"]; [Self.tabbaritem settitle: @" first Tab "];

But there is another way setfinishedselectedimage:withfinishedunselectedimage can display the icon in full, if the icon is too large will occupy the space below the text, and the icon will be displayed in the original color, The display of the graphic is also determined by the selected state of the tab.

[Self.tabbaritem setfinishedselectedimage:[uiimage imagenamed:@ "sunflower.gif"] Withfinishedunselectedimage:[uiimage imagenamed:@ "ixia.gif"];

When selected

When not selected

There is a Badgevalue property in each Tabbaritem that sets the value of the small red circle of the tab, which is nsstring, so the value that can be assigned to him is not limited to numbers, but other characters

self.tabbaritem.badgevalue=@ "new";

The number of tabs can be increased infinitely, but in the event it is possible to display a maximum of five tab tags, if the number of tabs is more than 5, the original tab will be a 5th , and clicking in is a navigation page with TableView, the tab starting from 5th will be displayed in TableView, and the tab page navigated from TableView will have a navigation bar to return to the Tabeview page .

The navigation bar also has the Edit button to enter the page with the Configuration tab

In this page, you can drag and drop tabs to the bottom of the Tabbar to change the Order of tab in Tabbar, and the tab shown here can be set by Uitabbarcontroller's Customizableviewcontrollers property. , by default it is the same element as the Viewcontroller property of Uitabbarcontroller (this is the collection that Uitabbarcontroller has tab). However, you can also remove the 6th tab from the Customizableviewcontrollers by setting a

Nsarray *viewcontrollers=*newviewcontrollers=[nsmutablearray arraywithcapacity:viewcontrollers.count- 1 ];  for (int i=0; i<[viewcontrollers count]-1; i++) {    [newviewcontrollers addobject:[ Viewcontrollers objectatindex:i]];} Self.tabBarController.customizableViewControllers=newviewcontrollers;

As a result, the 6th tab will not appear on the Configure page.

In the case of Viewcontrollers tab and Customizableviewcontrollers tab, the tab will not be removed from Tabbar in the edit state.

About the selected tab

With SelectedIndex and Selectedviewcontroller, you can get or set the selected tab, and for Settings SelectedIndex and Selectedviewcontroller, you can only set the tab that is selected , and it's not possible to switch the page over, and there's a special case for more tab.

    • When More,selectedindex is selected, it cannot get its exact index value, and Selectedviewcontroller can get its uitabbaritem;
    • If a value greater than or equal to 5 is assigned to SelectedIndex, tab will be selected more;
    • If you assign a tabbarcontroller.morenavigationcontroller to Selectedviewcontroller or the index value is greater than or equal to 5, the Uitatbaritem,tab is selected more

The Tabbar property of the Uitabbarcontroller can also be selecteditem to get the selected Uitabbaritem, but it does not give the SelectedItem an assignment, which throws an exception. Although Uitabbar has some ways to change its state, it cannot modify its state for Uitabbarcontroller Tabbar.

Events in Uitabbarcontroller

Uitabbarcontroller has a number of events that can be used to monitor tab changes, which requires Viewcontroller to implement the Uitabbarcontrollerdelegate protocol, Then set the delegate property of the Tabbarcontroller

Self.tabbarcontroller. delegate=self;

The event is classified into two categories, the one is the simple tabbar on the tab on the selected state of change before and after the departure

-(BOOL) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller Shouldselectviewcontroller: (UIViewController *  -(void) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller Didselectviewcontroller: ( Uiviewcontroller *) Viewcontroller

First trigger the former and then trigger the latter, the former has a Boolean type return value, if the return is false style= font-family:arial; >tab selectedindex except. Two parameters tabbarcontroller represents the starting tab viewcontroller " represents the target's tab

edit Span style= "font-family: the song Body;" >

of the operation

-(void ) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller Willbegincustomizingviewcontrollers: (Nsarray *-(void ) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller willendcustomizingviewcontrollers: (NSArray * ) Viewcontrollers changed: (BOOL) changed -(void ) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller didendcustomizingviewcontrollers: (NSArray *) Viewcontrollers changed: (BOOL) Changed   

willbegin ... is in the entry Edit interface is triggered, willend ... and the didend .... was clicked on the Done and then triggered successively.

iOS Learning Notes-multi-controller management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.