How to use the iOS navigation bar

Source: Internet
Author: User

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" >
    1. #import <UIKit/UIKit.h>
    2. @interface Viewcontroller:uiviewcontroller
    3. @end

Code in the VIEWCONTROLLER.M file:

[CPP]View Plaincopy
  1. #import "ViewController.h"
  2. @interface Viewcontroller ()
  3. @end
  4. @implementation Viewcontroller
  5. -(void) Viewdidload
  6. {
  7. [Super Viewdidload];
  8. additional setup after loading the view, typically from a nib.
  9. //Create a navigation bar
  10. Uinavigationbar *navbar = [[Uinavigationbar alloc] Initwithframe:cgrectmake (0, 0, 320, 44)];
  11. //Create a navigation bar collection
  12. Uinavigationitem *navitem = [[Uinavigationitem alloc] initwithtitle:nil];
  13. //Add a caption to this collection item, button
  14. //style: Set the style of the button, a total of three options
  15. //action: @selector: Set Button's Click event
  16. //Create a left button
  17. Uibarbuttonitem *leftbutton = [[Uibarbuttonitem alloc] initwithtitle:@"left" style:uibarbuttonitemstylebordered  Target:self Action: @selector (Clickleftbutton)];
  18. //Create a right button
  19. Uibarbuttonitem *rightbutton = [[Uibarbuttonitem alloc] initwithtitle:@"right" style:uibarbuttonitemstyledone target  : Self action: @selector (Clickrightbutton)];
  20. //Set the contents of the navigation bar
  21. [Navitem settitle:@"lingling lacquer"];
  22. //Add the navigation bar collection to the navigation bar, set the animation to close
  23. [NavBar Pushnavigationitem:navitem Animated:no];
  24. //Add the left and right two buttons to the navigation bar collection
  25. [Navitem Setleftbarbuttonitem:leftbutton];
  26. [Navitem Setrightbarbuttonitem:rightbutton];
  27. //Add all the contents of the title bar to the main view
  28. [Self.view Addsubview:navbar];
  29. //Finally release the control in memory to avoid memory leaks
  30. [Navitem release];
  31. [LeftButton release];
  32. [Rightbutton release];
  33. }
  34. -(void) ShowDialog: (NSString *) str
  35. {
  36. Uialertview *alert = [[Uialertview alloc] initwithtitle:@"This is a dialog box" Message:str delegate:self cancelbuttontitle:@  "OK" otherbuttontitles:nil];
  37. [Alert show];
  38. [Alert release];
  39. }
  40. -(void) Clickleftbutton
  41. {
  42. [Self showdialog:@ Click on theleft button of the navigation bar];
  43. }
  44. -(void) Clickrightbutton
  45. {
  46. [Self showdialog:@ Click theright button on the navigation bar];
  47. }
  48. -(void) Viewdidunload
  49. {
  50. [Super Viewdidunload];
  51. //Release any retained subviews of the main view.
  52. }
  53. -(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
  54. {
  55. return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);
  56. }
  57. @end

How to use the iOS navigation bar

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.