Uinavigationcontroller details 3 Toolbar

Source: Internet
Author: User

Original article: http://blog.csdn.net/totogo2010/article/details/7682131 special thank you.

1. Display Toolbar

Add the code in the-(void) viewdidload method of rootviewcontroller. m so that toobar is displayed.

 

[CPP]View plaincopy
  1. [Self. navigationcontroller settoolbarhidden: No animated: Yes];


 

2. Add uibarbuttonitem on the toolbar

Create several uibarbuttonitems and add them to the toolbar as arrays.

 

[CPP]View plaincopy
  1. Uibarbuttonitem * One = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemadd target: Nil action: Nil];
  2. Uibarbuttonitem * Two = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitembookmarks target: Nil action: Nil];
  3. Uibarbuttonitem * Three = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemaction target: Nil action: Nil];
  4. Uibarbuttonitem * Four = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemedit target: Nil action: Nil];
  5. Uibarbuttonitem * flexitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemflexiblespace target: Nil action: Nil];
  6. [Self settoolbaritems: [nsarray arraywithobjects: flexitem, one, flexitem, two, flexitem, three, flexitem, four, flexitem, nil];


Effect:

 

 

Note: using [self. navigationcontroller. toolbar setitems :( nsarray *) animated: <# (bool) #>] This method to add an item does not work. Now, when I add a toolbar dynamically, this will take effect.

 

3. dynamically add Toolbar

We add a dynamic toolbar in secondview.

Add in secondviewcontroller. h

 

[CPP]View plaincopy
  1. # Import <uikit/uikit. h>
  2. @ Interface secondviewcontroller: uiviewcontroller
  3. {
  4. Uitoolbar * toolbar;
  5. }
  6. @ End


Add in secondviewcontroller. m

 

 

[CPP]View plaincopy
  1. -(Void) viewdidload
  2. {
  3. [Super viewdidload];
  4. [Self. navigationcontroller settoolbarhidden: Yes animated: Yes];
  5. Uibarbuttonitem * addbutton = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemsearch target: Self action: @ selector (gotothridview :)];
  6. Toolbar = [[uitoolbar alloc] initwithframe: cgrectmake (0.0, self. view. frame. size. height-toolbar. frame. size. height-44.0, self. view. frame. size. width, 44.0)];
  7. [Toolbar setbarstyle: uibarstyledefault];
  8. Toolbar. autoresizingmask = uiviewautoresizingflexibletopmargin;
  9. [Toolbar setitems: [nsarray arraywithobject: addbutton];
  10. [Self. View addsubview: toolbar];
  11. // Do any additional setup after loading the view from Its nib.
  12. }

First, hide the toobar displayed during rootview.

 

[Self. navigationcontroller settoolbarhidden: yesanimated: Yes]; then, the new toolbar is added to the secondview and an item is set for toobar.

[Toolbarsetitems: [nsarrayarraywithobject: addbutton];

Barbuttonitem uses uibarbuttonsystemitemsearch. The effect is as follows:

4. Create a New thridview and jump from secondview

Commad + N create a thridviewcontroller,

This addbutton jumps to thridview

 

[CPP]View plaincopy
  1. -(Void) gotothridview :( ID) sender
  2. {
  3. Thridviewcontroller * thridview = [[thridviewcontroller alloc] init];
  4. [Self. navigationcontroller pushviewcontroller: thridview animated: Yes];
  5. Thridview. Title = @ "thrid View ";
  6. }


Jump second to third:

 

 

At this point, the uinavigationcontroller has been practicing almost.

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.