Uitabbarcontrolle Basic Use tutorials in IOS UI development _ios

Source: Internet
Author: User
Tags reserved

Basic use of Uitabbarcontroller

First, a brief introduction

Uitabbarcontroller and Uinavigationcontroller similar, Uitabbarcontroller can easily manage multiple controllers, easy to complete the switch between the controller, the typical example is QQ, micro-letter, etc. should be ⽤.

Second, the use of Uitabbarcontroller

1. Use steps:

(1) Initialization of Uitabbarcontroller

(2) Setting the Rootviewcontroller of UIWindow as Uitabbarcontroller

(3) Create the appropriate child controller (Viewcontroller)

(4) The handle controller is added to the Uitabbarcontroller

2. code example

Creates a new empty file and encodes it in the application proxy

YYAPPDELEGATE.M file

Copy Code code as follows:

//
Yyappdelegate.m
01-uitabbar Controller Basic Use
//
Created by Hole medical self on 14-6-7.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYAppDelegate.h"

@implementation Yyappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
1. Create window
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Self.window.backgroundColor = [Uicolor Whitecolor];

A. Initializing a Tabbar controller
Uitabbarcontroller *tb=[[uitabbarcontroller Alloc]init];
Setting the root controller for Windows
SELF.WINDOW.ROOTVIEWCONTROLLER=TB;

B. Creating a child controller
Uiviewcontroller *c1=[[uiviewcontroller Alloc]init];
C1.view.backgroundcolor=[uicolor Graycolor];
C1.view.backgroundcolor=[uicolor Greencolor];
c1.tabbaritem.title=@ "message";
C1.tabbaritem.image=[uiimage imagenamed:@ "Tab_recent_nor"];
c1.tabbaritem.badgevalue=@ "123";

Uiviewcontroller *c2=[[uiviewcontroller Alloc]init];
C2.view.backgroundcolor=[uicolor Browncolor];
c2.tabbaritem.title=@ "Contact";
C2.tabbaritem.image=[uiimage imagenamed:@ "Tab_buddy_nor"];

Uiviewcontroller *c3=[[uiviewcontroller Alloc]init];
c3.tabbaritem.title=@ "Dynamic";
C3.tabbaritem.image=[uiimage imagenamed:@ "Tab_qworld_nor"];

Uiviewcontroller *c4=[[uiviewcontroller Alloc]init];
c4.tabbaritem.title=@ "Setting";
C4.tabbaritem.image=[uiimage imagenamed:@ "Tab_me_nor"];


C. Add a child controller to Itabbarcontroller
The first way of C.1
[TB ADDCHILDVIEWCONTROLLER:C1];
[TB ADDCHILDVIEWCONTROLLER:C2];

The second way of C.2
TB.VIEWCONTROLLERS=@[C1,C2,C3,C4];


2. Set Windows as the main window and display it
[Self.window makekeyandvisible];
return YES;
}

@end


Implementation effect:


Iii. Important Notes

1.UITabBar

The tool bar below is called Uitabbar, and if Uitabbarcontroller has an n child controller, then there will be N Uitabbarbutton within Uitabbar as the child control corresponding to it.

Note: The position of Uitabbarbutton in Uitabbar is divided evenly, and the uitabbar height is 49.

In the above program, Uitabbarcontroller has 4 sub controllers, so Uitabbar has 4 Uitabbarbutton,uitabbar structure ⼤ roughly as shown in the following figure:

2.UITabBarButton

Uitabbarbutton⾥ surface ⽰ What content, by the corresponding sub controller's Tabbaritem attribute to decide

c1.tabbaritem.title=@ "message";
C1.tabbaritem.image=[uiimage imagenamed:@ "Tab_recent_nor"];

3. There are two ways to add a child controller to the Uitabbarcontroller

(1) [TB ADDCHILDVIEWCONTROLLER:C1];

(2) TB.VIEWCONTROLLERS=@[C1,C2,C3,C4];

Note: The order of presentation is the same as the order of the additions, and unlike the navigation controller, the view that is displayed in front of the controller is the first one added.

Uitabbarcontroller lifecycle (built using Storyoard)
First, Uitabbarcontroller in the Storyoard have to build
1. Create a new project, remove the default controller in the storyboard, and drag UITab Bar Controller.
2. Create Viewcontroller and add to UITab Bar Controller (Wired).

Note: The order of the lines is the order of the future display, shown in front of the first line of view.
Hint: the interface of the controller corresponds to the Tabbarbutton and the picture shows what content, determined by its controller.
3. Set the Uitabbar of the child controller and other information.

4. Operation effect

Second, the Uitabbarcontroller life cycle Demo
idea: Create three controller classes to manage the controller separately, and rewrite the internal lifecycle method to understand Uitabbarcontroller internal management mechanism.

Analysis Code:

Copy Code code as follows:

//
Yybaseviewcontroller.m
02-uitabbarcontroller
//
Created by Hole medical self on 14-6-8.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYbaseViewController.h"

@interface Yybaseviewcontroller ()

@end


Copy Code code as follows:

@implementation Yybaseviewcontroller

Called when the controller's view is loaded
-(void) viewdidload
{
[Super Viewdidload];
NSLog (@ "%@-the controller's view Loaded", [self class]);
}

Call when the controller is about to be displayed
-(void) Viewwillappear: (BOOL) animated
{
[Super Viewwillappear:yes];
NSLog (@ "%@-Controller will be displayed", [Self class]);
}

Call when the controller is fully displayed
-(void) Viewdidappear: (BOOL) animated
{
[Super viewdidappear:animated];
NSLog (@ "%@-Controller full display", [self class]);
}

Call when the controller is about to disappear
-(void) Viewwilldisappear: (BOOL) animated
{
[Super viewwilldisappear:animated];
NSLog (@ "%@-Controller is about to disappear", [self class]);
}
Call when the controller disappears completely
-(void) Viewdiddisappear: (BOOL) animated
{
[Super viewdiddisappear:animated];
NSLog (@ "%@-Controller disappears completely", [self class]);
}

-(void) viewwillunload
{
[Super Viewwillunload];
NSLog (@ "%@-view is about to be destroyed", [self class]);
}

-(void) viewdidunload
{
[Super Viewdidunload];
NSLog (@ "%@-view is completely destroyed", [self class]);
}

-(void) dealloc
{
NSLog (@ "%@", [Self class]);
}

@end


(1) Run the program, the printout is:

Note: When the three sub controllers are added to Uitabbarcontroller for management, it will only load the view of the first added controller when the program is started.
(2) Click on the contact button to switch to the second interface. The printout is:

Description: Move the first view away and add the new view, but the first view is not destroyed.
(3) Click the message interface again, print as follows:

Note: Switch to the message interface first, one controller is going to be shown directly, without loading proof (2) The first view removal was not destroyed (because its controller is still present, a strong reference is referenced), and two's view removal has not been destroyed. No matter how you switch, the controller and view will not be destroyed.
Uinavigationcontroller and Uitabbarcontroller are managed by stacks, one through a common array.

Supplementary note: The actual height of Uitabbar in Uitabbarcontroller is 49.
Print the Uitabbar frame for viewing in the application method below.

Copy Code code as follows:

-(void) Applicationdidbecomeactive: (uiapplication *) application
{
Restart any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.
Uitabbarcontroller *tb= (uitabbarcontroller*) Self.window.rootViewController;
NSLog (@ "%@", Nsstringfromcgrect (Tb.tabBar.frame));
}

Printing results are:

Related Article

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.