Uitabbarcontroller use of the detailed
Uitabbarcontroller is a very common viewcontroller in iOS, such as the system's alarm clock programs, ipod programs, and so on. Uitabbarcontroller are usually rootviewcontroller for the entire program and cannot be added to other container viewcontroller.
First, let's take a look at its view level graph:
First, manually create Uitabbarcontroller
The most common place to create Uitabbarcontroller is the applicationdidfinishlaunching in the application delegate : Method, Since Uitabbarcontroller is usually the rootviewcontroller of the entire program, we need to create it before the window of the program is displayed, in the following steps:
1. Create a Uitabbarcontroller object
2. Create an object to display for each tab in the Tabbarcontroller
3. Add all content Viewcontroller to Uitabbarcontroller by Uitabbarcontroller's Viewcontroller property
4. By setting the Uitabbarcontroller object to Window.rootviewcontroller, and then displaying the window
Let's look at a simple example:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease]; Override point for customization after application launch.
Svtabbarfirstviewcontroller *viewcontroller1, *viewcontroller2;
ViewController1 = [[Svtabbarfirstviewcontroller alloc] Initwithnibname:nil Bundle:nil]; Viewcontroller1.title = @ "First"; ViewController2 = [[Svtabbarfirstviewcontroller alloc] Initwithnibname:nil Bundle:nil]; Viewcontroller2.title = @ "Second"; Self.tabbarcontroller = [[[Uitabbarcontroller alloc] init] autorelease]; Self.tabBarController.delegate = self; Self.tabBarController.viewControllers = [Nsarray arraywithobjects:viewcontroller1, ViewController2, nil];
[ViewController1 release]; [ViewController2 release]; Self.window.rootViewController = Self.tabbarcontroller; [Self.window makekeyandvisible]; return YES;}
Second, Uitabbaritem
Uitabbar Each tab shown above corresponds to a viewcontroller, and we can change the tab display on Tabbar by setting the Viewcontroller.tabbaritem property. Otherwise the system will automatically create one according to the title of Viewcontroller, the Tabbaritem only display text, no images. When we create our own uitabbaritem, we can display the specified displayed image and the corresponding text description. Of course can also through Setfinishedselectedimage:withfinishedunselectedimage: method to specify a different picture for the selected state and the Fly check state. Here's a small example of creating Uitabbaritem yourself:
Uitabbaritem *item = [[Uitabbaritem alloc] Initwithtitle:@ "Second" Image:nil tag:2];[ Item Setfinishedselectedimage:[uiimage imagenamed:@ "second.png"] withfinishedunselectedimage:[ UIImage imagenamed:@ "first.png"]];viewcontroller2.tabbaritem = item;[ Item release];
Uitabbaritem also has an attribute badgevalue, which can be set to display a small corner mark in its upper-right corner, usually to prompt the user for a new message, which is simple to use, followed by an example.
Third,Morenavigationcontroller
Uitabbar can display up to 5 tab, when we add more than Uitabbarcontroller to the Viewcontroller more than 5 times, the last one will automatically become, in accordance with the order of the Viewcontrolles set, Displays the Tabbaritem of the first four Viewcontroller, and the subsequent tabbaritem will no longer be displayed. When you click More, a standard navigationviewcontroller will pop up with the other non-displayed viewcontroller, with an edit button, By clicking on this button you can enter an editing interface similar to the settings tabbar in the ipod program. All viewcontroller in the editing interface can be edited, and we can specify the Uitabbarcontroller by setting the Customizableviewcontrollers property of the A subset of Viewcontrollers that allows only part of the Viewcontroller to be displayed in Tabbar. But one problem with this piece is that whenever Uitabbarcontroller's viewcontrollers attribute changes, Customizableviewcontrollers will automatically be set to the same as viewcontrollers, that is, all of the default Viewcontroller are editable, if we want to always limit only a certain part can be edited, Remember to reset the customizableviewcontrollers once every time the viewcontrolles has changed.
Iv. the rotation of Uitabbarcontroller
Uitabbarcontroller only supports vertical screens by default, and when the device direction changes, it queries all viewcontroller contained in the viewcontrollers, only if all viewcontroller support that direction. Uitabbarcontroller only occurs when the rotation is not the default vertical.
It is important to note that when Uitabbarcontroller supports rotation and rotates, only the currently displayed Viewcontroller receives the rotated message.
Wu, Uitabbar
Uitabbar yourself There are some ways to change your state, but for the tabbar of Uitabbarcontroller, we cannot directly modify its state. Any action that directly modifies Tabbar will throw an exception, following a small example of throwing an exception:
Self.tabbarcontroller = [[[Uitabbarcontroller alloc] init] autorelease];self.tabbarcontroller. Delegate = self;self.tabbarcontroller.viewcontrollers = [Nsarray arraywithobjects:viewcontroller1, ViewController2, ViewController3, nil]; Self.window.rootViewController = self.tabbarcontroller;[ Self.window makekeyandvisible]; Self.tabBarController.tabBar.selectedItem = nil;
The last line of the above code modifies the state of the Tabbar directly, and the running program returns the following result:
Vi. Change Selected Viewcontroller
Changing the Viewcontroller currently displayed in the Uitabbarcontroller can be done in two ways:
1. SelectedIndex Properties
This property allows you to get the currently selected Viewcontroller and set this property to display the Viewcontroller of the corresponding index in Viewcontrollers. If Moreviewcontroller is currently selected, the value obtained by this property is Nsnotfound, and the property cannot be set with the selected Moreviewcontroller. Setting index beyond the range of viewcontrollers will be ignored.
2. Selectedviewcontroller Properties
This property allows you to get to the currently displayed Viewcontroller, set this property to set the currently selected Viewcontroller, and update selectedindex. You can assign a value to this property by assigning
Tabbarcontroller. Morenavigationcontroller can select Moreviewcontroller.
3. Viewcontrollers Properties
Setting the Viewcontrollers property also affects the currently selected Viewcontroller, and when you set this property Uitabbarcontroller first clears all the old Viewcontroller. Then deploy the new Viewcontroller, then try to re-select the last displayed Viewcontroller, and if the Viewcontroller does not already exist, then try to select the index and SelectedIndex phase Viewcontroller, if the index is invalid, the first Viewcontroller is selected by default.
Seven, uitabbarcontrollerdelegate
Through the agent we can monitor the changes of Uitabbarcontroller's currently selected Viewcontroller and edit all Viewcontroller in Moreviewcontroller. By implementing the following methods:
-(BOOL) Tabbarcontroller: (Uitabbarcontroller *) Tabbarcontroller Shouldselectviewcontroller: (UIViewController *) Viewcontroller;
This method is used to control whether Tabbaritem can be selected and returns no, which prevents the user from clicking on a tabbaritem selected. However, the Tabbaritem can still be selected within the program by direct setSelectedIndex.
The following three methods are mainly used to monitor the edit operation of the view controller in Moreviewcontroller
-(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;
Original URL: http://www.cnblogs.com/smileEvday/archive/2012/05/20/UITabBarController.html
iOS Development Uitabbarcontroller