How to Create a title bar uinavigationbar for iOS beginners (2)

Source: Internet
Author: User
Use of uinavigationbar to create a title bar for iOS beginners

Yu Song Momo Original article reproduced, Please Note: Reproduced to my independent domain name blog Yu Song Momo program Research Institute, original address: http://www.xuanyusong.com/archives/585
For iOS development, you can use not only code but also interface builder visual tools. Today, a friend asked me what are the differences between the two. First, let's talk about the difference between the two. The underlying layer of the control edited by IB and the calling code are only encapsulated by Apple for better use by developers. Its advantage is convenient and quick. The most important thing is security, because the release of the control will help us do not need to manually release it. The disadvantage is that development by many people is not easy to maintain, just like who writes IB and who can understand it, it is hard for others to read it, and it is not conducive to code maintenance. The two methods have their own advantages and disadvantages, but I personally prefer pure code because code and visualization tools are always the lowest layer in any programming language or any scripting language.

Use the code to add a title bar to the screen, and add two buttons to the left and right sides of the title bar. Then, click and respond to the two buttons.
Set the display range of the title bar.

// Create a navigation bar uinavigationbar * navigationbar = [[uinavigationbar alloc] initwithframe: cgrectmake (0, 0,320, 44)];

With the title bar, you must add a set item on the title bar to place the title content and buttons.

// Create a navigation bar set uinavigationitem * navigationitem = [[uinavigationitem alloc] initwithtitle: Nil];

Add the title and button to this set item.

Style: Set the button style, which is selected from 3 in total.
Action: @ selector: Set the button to click the 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 navigation bar content [navigationitem settitle: @ "Yu Song Momo Program World"];

Add all content in the title bar to the main view.

// Add the navigation bar to the view [self. View addsubview: navigationbar];

Finally, release the control in the memory to avoid Memory leakage.

// Release the object [navigationitem release]; [leftbutton release]; [rightbutton release];

: Add the two button click response events.

-(Void) clickleftbutton {[self showdialog: @ "click the button on the left of the navigation bar"];}-(void) clickrightbutton {[self showdialog: @ "click the button on the right of the navigation bar"];}

Click it to open a dialog box, and input different display content based on different buttons.

-(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];}

Finally, paste the complete code

# Import "titleviewcontroller. H "@ implementation titleviewcontroller-(void) didreceivememorywarning {// releases the view if it doesn' t have a superview. [Super didreceivememorywarning]; // release any cached data, images, etc that aren't in use .} # pragma mark-view lifecycle // implement viewdidload to do additional setup after loading the view, typically from a nib. -(void) viewdidload {[Super viewdidload]; // create a navigation bar uinavigationbar * navigationbar = [[uinavigationbar alloc] initwithframe: cgrectmake (0, 0,320, 44)]; // create a navigation bar set uinavigationitem * navigationitem = [[uinavigationitem alloc] initwithtitle: Nil]; // 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 navigation bar content [navigationitem settitle: @ "Yu Song Momo Program World"]; // Add the navigation bar set to the navigation bar and set the animation to close [navigationbar pushnavigationitem: navigationitem animated: No]; // Add the left and right buttons to [navigationitem setleftbarbuttonitem: leftbutton]; [navigationitem setrightbarbuttonitem: rightbutton]; // Add the navigation bar to the view [self. view addsubview: navigationbar]; // release object [navigationitem release]; [leftbutton release]; [rightbutton release];}-(void) clickleftbutton {[self showdialog: @ "click the button on the left of the navigation bar"];}-(void) clickrightbutton {[self showdialog: @ "click the button on the right of the navigation bar"];}-(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) viewdidunload {[Super viewdidunload]; // release any retained subviews of the main view. // e.g. self. myoutlet = nil;}-(bool) returns :( uiinterfaceorientation) interfaceorientation {// return YES For supported orientations return (interfaceorientation = uiinterfaceorientationportrait);} @ end

In the end, if you still think that I am not writing enough details, it doesn't matter if I post the source code. You are welcome to discuss and study Yu Song Momo, hoping to make progress together with everyone.



: Http://www.xuanyusong.com/archives/585

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.