This article is the effect of using pure code to implement a navigation bar. Click the button and generate the event. The basic ideas are:
1. Create a navigation bar (Uinavigationbar object)
2. Create a navigation bar collection (Uinavigationitem object)
3. Create a left button, a right button (Uibarbuttonitem object), and implement the corresponding event method
4. Add a navigation bar collection to the navigation bar, set the animation to close
5. Add the left and right two buttons to the navigation bar collection
6. Display the currently created navigation bar in the view
The specific implementation code is as follows:
The code in the ViewController.h file does not have to change, as follows:
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- #import <UIKit/UIKit.h>
- @interface Viewcontroller:uiviewcontroller
- @end
Code in the VIEWCONTROLLER.M file:
[CPP]View Plaincopy
- #import "ViewController.h"
- @interface Viewcontroller ()
- @end
- @implementation Viewcontroller
- -(void) Viewdidload
- {
- [Super Viewdidload];
- additional setup after loading the view, typically from a nib.
- //Create a navigation bar
- Uinavigationbar *navbar = [[Uinavigationbar alloc] Initwithframe:cgrectmake (0, 0, 320, 44)];
- //Create a navigation bar collection
- Uinavigationitem *navitem = [[Uinavigationitem alloc] initwithtitle:nil];
- //Add a caption to this collection item, button
- //style: Set the style of the button, a total of three options
- //action: @selector: Set Button's Click event
- //Create a left button
- Uibarbuttonitem *leftbutton = [[Uibarbuttonitem alloc] initwithtitle:@"left" style:uibarbuttonitemstylebordered Target:self Action: @selector (Clickleftbutton)];
- //Create a right button
- Uibarbuttonitem *rightbutton = [[Uibarbuttonitem alloc] initwithtitle:@"right" style:uibarbuttonitemstyledone target : Self action: @selector (Clickrightbutton)];
- //Set the contents of the navigation bar
- [Navitem settitle:@"lingling lacquer"];
- //Add the navigation bar collection to the navigation bar, set the animation to close
- [NavBar Pushnavigationitem:navitem Animated:no];
- //Add the left and right two buttons to the navigation bar collection
- [Navitem Setleftbarbuttonitem:leftbutton];
- [Navitem Setrightbarbuttonitem:rightbutton];
- //Add all the contents of the title bar to the main view
- [Self.view Addsubview:navbar];
- //Finally release the control in memory to avoid memory leaks
- [Navitem release];
- [LeftButton release];
- [Rightbutton release];
- }
- -(void) ShowDialog: (NSString *) str
- {
- Uialertview *alert = [[Uialertview alloc] initwithtitle:@"This is a dialog box" Message:str delegate:self cancelbuttontitle:@ "OK" otherbuttontitles:nil];
- [Alert show];
- [Alert release];
- }
- -(void) Clickleftbutton
- {
- [Self showdialog:@ Click on theleft button of the navigation bar];
- }
- -(void) Clickrightbutton
- {
- [Self showdialog:@ Click theright button on the navigation bar];
- }
- -(void) Viewdidunload
- {
- [Super Viewdidunload];
- //Release any retained subviews of the main view.
- }
- -(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
- {
- return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);
- }
- @end
How to use the iOS navigation bar