IOS study 5: uitabbarcontroller preliminary

Source: Internet
Author: User

Uitabbarcontroller is a common control in the project architecture.

Let's take a rough look at the effect of the control, so we can see why it is common.

This is the simplest prototype. It is believed that the basic application interface structure of 70% is like this.

In Android, we implement activitygroup or the current fragment. A container contains multiple sub-controllers.

The following is an example of the overall layout in the form of an XIB file.

Of course, in xcode4.3.2, we will find that such a template exists directly.

However, after directly using the template, we will find that the sub-layout has to be added directly in the Code. Because we are not skilled, We can customize the image text of item, tabbar, or something, the API in the Code cannot be found at once,

It may be said that the XIB implementation can be quite clear.

It is said that there was a base_window template directly in the past, but it doesn't matter. The template is just for me to quickly create an application. Here we start from the most basic manual

1. First create an empty application

2. after the creation, we will customize a mainwindow. XIB (of course, this name can be obtained at will, but according to the specifications and a default habit), as the first NIB file loaded when the application starts,

You can choose either window or empt to create a new XIB file. In fact, they are similar. Here we select the window template to obtain the XIB file.

3. Then we will connect xxxappdelegate to the xib file. Therefore, the. h file is defined as follows:

# Import <uikit/uikit. h> @ interface nonoappdelegate: uiresponder <uiapplicationdelegate> {uiwindow * window; uitabbarcontroller * tabtarcontroller;} @ property (retain, nonatomic) iboutlet uiwindow * window; // This control template does not contain iboutlet. However, for the uniformity of the XIB file layout, we can use it as an output port and connect it to mainxib. @ property (retain, nonatomic) iboutlet uitabbarcontroller * tabtarcontroller; @ end

4. Then let's roughly design our XIB file and open the mainwindow. XIB file. We can see the view elements, one is the file owner filesowner, and the other is reponder (this has never been

How to use it, the specific reason is not clear), and then the most important thing is to get the elements under the Object Label, this is a window.

First, we need to change the class of the file owner to uiapplication. Click the file 'sowner label and select indentity inspector in the attribute column on the right. Then, we can see that customclass is nsobject,

We changed it to uiapplication. After the modification, we will find that file 'sowner has outlets used a delegate output port. We will know from the previous article about outlets and reference outlet,

This stuff can point to an instance object or something.

5. Now, we can add a delegate object under the object. The operation is very simple. In the control group on the right

Drag an object like this to the object tag under XIB,

Then we can customize this object. As we know above, we can roughly know that we need an object similar to the delegate class, right

Isn't our appdelegate just such a thing. Naturally, select this object and set custom class to nonoappdelagate In the attribute column on the right.

Then click the file's owner to link the output port delegate with the nonoappdelagate we just placed.

6. after setting the preceding settings, we can click xxxappdelegate under the object and view outlets in the attribute column on the right. By the way, we just got it in this file. h declares two output ports. Now we are

Create two such objects and connect them. Window already exists, and there are fewer uitabbarcontrollers. Let's drag one from the right.

The output port is connected to the object.

At this moment, the most basic tabbarcontroller layout framework is OK. The XIB file at this moment is as follows:

Then we open appdelegate. m for implementation and modification.

#import "NonoAppDelegate.h"@implementation NonoAppDelegate@synthesize window ;@synthesize tabTarController ;- (void)dealloc{    [self.window release];    [self.tabTarController release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{//    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    self.window.rootViewController = self.tabTarController;    [self.window makeKeyAndVisible];    return YES;}

Well, it's that simple.

Theoretically, the program can be started after the simulator is started.

However, it is blank after discovery.

We can see from the main. m of the application

int main(int argc, char *argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, NSStringFromClass([NonoAppDelegate class]));    }}

Then we applied the custom delegate class and found a problem. According to this process, the mainwindow. XIB file does not seem to be loaded.

That's right. In the past, we defined a controller's XIB file with initwithnibname, and for the uiapplication-type XIB file (in fact, the entire application is the first class to be started ),

It is said that this is the way to load

For details, you can refer to the iOS app startup content mentioned in the previous article. There is a good blog that gives a good explanation.
At this moment, we can slightly modify the main. H code.

#import <UIKit/UIKit.h>#import "NonoAppDelegate.h"int main(int argc, char *argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, nil);    }}

The fourth parameter was originally set to appdelegate, but we know that when loading a delegate object in mainwindow. XIB instantiation, We have linked the appdelegate class,

It seems that after the fourth parameter is specified, this class is instantiated twice. For more details, refer to the blog mentioned above.

7. All right. The tabbar interface is the most basic step. Next, add a sub-controller to it.

First, familiarize yourself with the uitabbarcontroller control:

We can infer from the interface that there is something in the control:

1>. There should be something similar to managing a group of child controls.

2> you have to switch the tabbar under the interface.

3> Of course, I also found a delegate object.

Of course, the above is in the code. We get these attributes and perform corresponding operations.

It may seem intuitive in the xib file.

1> the tab Bar contains an array composed of one or more tabbaritems. Each ITM corresponds to a viewcontroller.

2> the first, second, and so on are the controllers corresponding to each item. Pay attention to this point. By default, we drag

Tabbaritem. Generally, the xxxcontroller. XIB file is set.


Then you need to change custom class to the xxxxcontroller class, because the default class is uiviewcontroller. This will report an error after the application is started.

3> title can be set in tabbaritem. The default icons include custom icons and badgevalue, which are the red values of the above labels,

This is more insightful than Android.

4> when there are more than five buttons at the bottom, the system will automatically add a more button. After you click more, the remaining buttons will be displayed.

The above is the simplest structure of the entire tanbar. The sub-controller uses the uiviewcontroller provided by the API.

However, the combination of tabarcontroller and navbar is basically the most popular mode on IOS.

More detailed tabbar operations need to be implemented in the code.

The tabbarcontroller View Controls are easy to use.

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.