Explain the basic usage of Uitabbarcontroller components in IOS app _ios

Source: Internet
Author: User
Tags uikit

Uitabbarcontroller This controller is definitely a common control for project schemas.
We have a rough look at the effect of the control and we know why he is common.

This is the simplest form of a prototype, presumably now basic 70% of the application interface structure will be like this.
In Android we implement Activitygroup or now fragment, a container containing multiple child controllers.
Here's an example of this overall layout in the form of creating a Xib file.
Of course, in the Xcode, we'll find that there's a template right there.

But the direct use of the template will be found directly in the code to achieve a child layout to add, because we are not skilled, for item,tabbar what the picture text customization, and suddenly can not find the code in the API,
It may be said that using xib to achieve can be seen more clearly.
It is said that there used to be a base_window template, but it doesn't matter, the template is just for me to quickly build an application, our side manually from the most basic to start
1. First establish a empty application

2. After the establishment of our own top a mainwindow.xib (of course, this name can be arbitrarily taken, but according to the specification and a default to stay in the habit), as the application started loading the first nib file,
In the new Xib file you can choose window can also be empt what, in fact, all the same, we choose the window template Xib file

3. Then we're actually going to connect xxxappdelegate with this xib file. So the. h file is defined as this:

Copy Code code as follows:

#import <UIKit/UIKit.h>

@interface Nonoappdelegate:uiresponder <UIApplicationDelegate>
{
UIWindow *window;
Uitabbarcontroller *tabtarcontroller;
}

@property (Retain, nonatomic) Iboutlet UIWindow *window;//The control template generation is not iboutlet, but we have the uniformity for Xib file layout. It can also be used as a outlet and connected in Mainxib
@property (Retain, nonatomic) Iboutlet Uitabbarcontroller *tabtarcontroller;
@end


4. Then we come to roughly design our Xib file, open the Mainwindow.xib file, we see the view element roughly, one is the file owner Filesowner, then Reponder (this has never
How to use, specifically why make is not clear, and then the most important is the object of the tag element, at this time a window.
First we want to change the owner of this class to UIApplication, click on the file ' Sowner tag in the right side of the property bar to select the Indentity inspector, see Customclass is nsobject at this time,
We changed to UIApplication, after the change will find file ' Sowner to outlets used a delegate export Dongdong, read the previous on outlets and reference outlet we know,
This dongdong can point to an instance object or something.

5. Well, then we add a delegate object under object, which is very simple, in the right control group

Drag one of these objects to the Xib under the Object tab,

And then we're going to come up with this object, and according to the above, we can basically know that we need an object similar to the delegate class,

Our appdelegate is just such a thing. So it's natural to select this object and then set the custom class to nonoappdelagate in the right-hand property bar.

Then click File ' owner to link its output port delegate with the nonoappdelagate we just put up.


6. After setting up, we can click on the object to Xxxappdelegate, and then look at the right side of the property bar Outlets, right, we have just in the document has been declared two exports, now we are

To create two of these objects and then connect them. Window was already there, and a few uitabbarcontroller. So, we're dragging from the right.

Then the outlet and the object link up.

At the moment, the most basic Tabbarcontroller layout frame is OK, the Xib file at the moment as shown below

Then we open the APPDELEGATE.M for implementation and modification.

Copy Code code as follows:

#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 we start the simulator now and the program can start.
But it was found to be blank.
We see from the MAIN.M of application
Copy Code code as follows:

int main (int argc, char *argv[])
{
@autoreleasepool {
Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Nonoappdelegate class]);
}
}

Then applied to our custom delegate class, and then found a problem, according to this process, we mainwindow.xib file seemingly did not load in AH.
That's right, once we've defined a controller xib file there will be Initwithnibname, and for uiapplication type of xib file (in fact, a class that starts the whole application first),
is said to be loaded like this

This specific you can go to see in one of the mentioned iOS app launch content, there is a good blog to give a good explanation.
Now we can modify the Main.h code slightly

Copy Code code as follows:

#import <UIKit/UIKit.h>

#import "NonoAppDelegate.h"

int main (int argc, char *argv[])
{
@autoreleasepool {
Return Uiapplicationmain (argc, argv, nil, nil);

}
}


The fourth parameter is the specified appdelegate, but we know that we have linked the Appdelegate class when we load the Mainwindow.xib instantiation of a delegate object.
Feel like if the fourth parameter is specified, this class is instantiated 2 times, more specific content or refer to the blog to see.

7. Ok. The most basic Tabbar interface is complete, next is to add the child controller inside.
First, let's familiarize ourselves with the following Uitabbarcontroller control:
From the interface we can extrapolate that there is something in the control that is roughly:
1> there should be something similar to managing a set of child controls.
2> interface switch under the Switch bar bar Tabbar
3> Of course also found a delegate such a thing, is the proxy object.
Of course the above is in the code, we get these properties, do the appropriate operation.
In the Xib file, it might look more intuitive.

1>tab bar contains an array of one or more tabbaritem, each itm corresponding to a viewcontroller.
2> below First,second and so on is each item corresponding controller, here also should note that, the default us we drag in a
Tabbaritem, generally we will set the corresponding Xxxcontroller.xib file

The custom class must then be changed to the corresponding Xxxxcontroller class, because the default class is Uiviewcontroller. This will be an error after the application is started.
The 3>tabbaritem can be set to title, the system's default icons, and custom icons, and Badgevalue, which is each red value on the label above,
This is much more prescient than Android, huh?
4> when the button at the bottom of more than 5, the system will automatically add a more button, click More, the remaining button will be displayed.

8.UITabbarController Sliding Switch tab
The following code is added to each Tabbar Viewcontroller, and it is recommended to add it in the base class:

Copy Code code as follows:

Viewdidload
Uiswipegesturerecognizer *swipeleft = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Tappedrightbutton:)];

[Swipeleft Setdirection:uiswipegesturerecognizerdirectionleft];

[Self.view Addgesturerecognizer:swipeleft];

Uiswipegesturerecognizer *swiperight = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Tappedleftbutton:)];

[Swiperight Setdirection:uiswipegesturerecognizerdirectionright];

[Self.view Addgesturerecognizer:swiperight];

Add 2 more functions, including toggle animation effect:

Copy Code code as follows:

-(Ibaction) Tappedrightbutton: (ID) sender

{

Nsuinteger SelectedIndex = [Self.tabbarcontroller selectedindex];

Nsarray *aryviewcontroller = self.tabBarController.viewControllers;

if (SelectedIndex < aryviewcontroller.count-1) {

UIView *fromview = [Self.tabBarController.selectedViewController view];

UIView *toview = [[Self.tabBarController.viewControllers objectatindex:selectedindex + 1] view];

[UIView transitionfromview:fromview toview:toview duration:0.5f options: Uiviewanimationoptiontransitionflipfromright completion:^ (BOOL finished) {

if (finished) {

[Self.tabbarcontroller Setselectedindex:selectedindex + 1];

}

}];

}

}


Copy Code code as follows:

-(Ibaction) Tappedleftbutton: (ID) sender

{

Nsuinteger SelectedIndex = [Self.tabbarcontroller selectedindex];

if (SelectedIndex > 0) {

UIView *fromview = [Self.tabBarController.selectedViewController view];

UIView *toview = [[Self.tabBarController.viewControllers objectatindex:selectedindex-1] view];

[UIView transitionfromview:fromview toview:toview duration:0.5f options: Uiviewanimationoptiontransitionflipfromleft completion:^ (BOOL finished) {

if (finished) {

[Self.tabbarcontroller setselectedindex:selectedindex-1];

}

}];

}


}

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.