IOS development note _ 3. uinavigationcontroller hierarchy

Source: Internet
Author: User

Generally, the navigation controller contains four objects: uinavigationcontroller, uinavigationbar, uiviewcontroller, and uinavigationitem;

Navigationitem is stored on uinavigationbar. We can know that a navigation controller controls multiple views, and a view controller controls a uinavigationitem.

Initialize navigationcontroller

RootViewController *rootViewController = [[RootViewController alloc] init];//    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];//    //    [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background.png"] forBarMetrics:UIBarMetricsDefault];        NavigationViewController *navigationController = [[NavigationViewController alloc] initWithRootViewController:rootViewController];

Navigationviewcontroller inherits the uinavigationcontroller

 

 

Navigationbar

One navigation controller controls multiple views. The leftitem, rightitem, and title on the navigationbar are controlled by the current view controller. Let's take a look at the creation method:

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(study)];    self.navigationItem.leftBarButtonItem = leftItem;    [leftItem release];

Let's take a look at self. navigationitem. leftbarbuttonitem indicates a hierarchical relationship. items are controlled by the navigationitem of the current view controller. If they are written as self. navigationcontroller. navigationitem. leftbarbuttonitem does not work, which is equivalent to adding leftitem to each view controller, which violates our original intention!

    if([self.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){        [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background.png"] forBarMetrics:UIBarMetricsDefault];    }

This statement can only be used in Versions later than ios5, and a background image is set for navigationbar.

 

Toolbar

The usage of toolbar is similar to that of navigationbar, except that toolbar is managed by array. This is very important and hidden by default. Remember to set it to open if you want to use it.

Uibarbuttonitem * tbaritem = [[uibarbuttonitem alloc] lower: lower target: Self action: @ selector (book)]; uibarbuttonitem * tbaritem1 = [[uibarbuttonitem alloc] lower: lower target: self action: Nil]; uibarbuttonitem * tbaritem3 = [[uibarbuttonitem alloc] initwithtitle: @ "title" style :( uibarbuttonitemstyledone) Target: Self action: Nil]; uibarbuttonitem * tbaritem4 = [[uibarbuttonitem alloc] initwithtitle: @ "button1" style :( uibarbuttonitemstylebordered) Target: Self action: Nil]; // set the interval uibarbuttonitem * flex = [[uibarbuttonitem alloc] lower: lower target: Self action: Nil]; nsarray * itemarray = @ [tbaritem, flex, tbaritem1, flex, tbaritem3, flex, tbaritem4]; // The default value is hidden [self. navigationcontroller settoolbarhidden: No animated: Yes]; [self settoolbaritems: itemarray];

Be sure to open it, otherwise it will not be displayed!

 

Set the display of navigationbar and tabbar

[self.navigationController setToolbarHidden:NO animated:YES];    [self.navigationController setNavigationBarHidden:NO animated:YES];

 

 

 

 

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.