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
- [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
- Uibarbuttonitem * One = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemadd target: Nil action: Nil];
- Uibarbuttonitem * Two = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitembookmarks target: Nil action: Nil];
- Uibarbuttonitem * Three = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemaction target: Nil action: Nil];
- Uibarbuttonitem * Four = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemedit target: Nil action: Nil];
- Uibarbuttonitem * flexitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemflexiblespace target: Nil action: Nil];
- [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
- # Import <uikit/uikit. h>
- @ Interface secondviewcontroller: uiviewcontroller
- {
- Uitoolbar * toolbar;
- }
- @ End
Add in secondviewcontroller. m
[CPP]View plaincopy
- -(Void) viewdidload
- {
- [Super viewdidload];
- [Self. navigationcontroller settoolbarhidden: Yes animated: Yes];
- Uibarbuttonitem * addbutton = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemsearch target: Self action: @ selector (gotothridview :)];
- 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)];
- [Toolbar setbarstyle: uibarstyledefault];
- Toolbar. autoresizingmask = uiviewautoresizingflexibletopmargin;
- [Toolbar setitems: [nsarray arraywithobject: addbutton];
- [Self. View addsubview: toolbar];
- // Do any additional setup after loading the view from Its nib.
- }
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
- -(Void) gotothridview :( ID) sender
- {
- Thridviewcontroller * thridview = [[thridviewcontroller alloc] init];
- [Self. navigationcontroller pushviewcontroller: thridview animated: Yes];
- Thridview. Title = @ "thrid View ";
- }
Jump second to third:
At this point, the uinavigationcontroller has been practicing almost.