Use of the Tab Bar for iOS learning and view switching

Source: Internet
Author: User

In iOS programs, the usage of Tab Bar is very high. When several views need to be switched, tabbar is used.

Today's program achieves this. There are several tab items at the bottom, corresponding to several views, switching to the tab Item, and switching to the corresponding view.

The implementation result is as follows:

 

To better understand how to use tabbar and Switch views, we create an Empty Application.

1. Open Xcode and create a project.

2. Create a View Controller

In the project, press spline + N to create a new file, create an Objective-C class file, press Next, and select UIViewController for subClass. Select the xib Option

Create three other viewcontrollers, RedViewController, GreyViewController, and YellowViewController in the same way. Four Views are ready. What about Tabbar?

3. Create the TabBarController. xib file and select create Empty file.

At this time, you find that the created xib file is blank and you don't have to worry about it. Drag the TabBar Controller in the control bar in the lower right corner and it will be OK.

4. Associate the TabBarController. xib and tabbarAppDelegate files.

Select File's Owner in, open Identity Inspector, and select tabbarAppDelegate in the Class column.

 

In this way, we can create an Outlet let ing between the TabBarController. xib file and the tabbarAppDelegate file.

5. Open the Assistant Editor in the View menu on the toolbar of Xcode so that tabbarAppDelegate. h and TabBarController. xib can be opened at the same time.

Press and hold the control key on the xib file to go to tabbarAppDelegate. h and create Outlet.

In the pop-up window, enter rootController and click connect.

6. Add code

Open tabbarAppDelegate. m and add the code in the didfinishlaunchingwitexceptions method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    [[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];    [self.window addSubview:self.rootController.view];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}

7. Add the Tab Bar Item to TabBarController. xib,

Drag the Tab Bar Item control on the control Bar to TabBarController. xib and hold four items at a time.

8. Associate the Tab Bar Item and *** ViewController.

Select one of the tabs, open Identity Inspector in the upper right corner, and select BlueViewController in the Class:

Then, open Attribute and select BlueViewController in NIB Name.

The other three tab items repeat similar operations and select the corresponding ViewController. In this way, you can switch to the corresponding page when switching the Tab. 9. Set the attributes of the tab itemSelect one of the tabs. The following information is displayed in the attribute bar in the upper right corner.

Badge is a number in the Red Circle, indicating how many pieces of information the attribute Identifier is the tab item style, select m as the custom, the following is the system style. I have selected four of them. The title image of bar ITem can be set under the custom style. 10. make similar settings for the remaining three Tab items. Now it's basically finished. Run it to see how it works. Well, in fact, it is the same as the first and second figure. We will not release it here. 11. Add a Log to the viewDidLoad method to observe and switch the View. You can add a Log to see when the corresponding View is running. The first running View is BlueViewController. When other tab items are clicked, other views are loaded. After loading the view once, viewDidLoad is no longer called next time.
- (void)viewDidLoad{    [super viewDidLoad];    NSLog(@"BlueViewController");    // Do any additional setup after loading the view from its nib.}

Get code: https://github.com/schelling/YcDemo

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.