Uinavigationcontroller and use of iOS learning (i) Add Uibarbuttonitem

Source: Internet
Author: User

1. How to use the Uinavigationcontroller navigation controller

Uinavigationcontroller can be translated as a navigation controller, which is often used in iOS.

Let's see how it's used:

The following figure shows the flow of the navigation controller. The leftmost is the root view, which slides into the screen when the user taps the general item, and the Auto-lock view slides into the screen when the user continues to tap the Auto-lock item. Accordingly, on object management, the navigation controller uses the navigation stack. The root view controller is at the bottom of the stack, and the next stack is the general view Controller and the Auto-lock view controller. You can call the pushviewcontrolleranimated: method to push the view controller into the top of the stack, or you can call the popviewcontrolleranimated: method to eject the view controller.

From Apple's official website.

2, the structure of the uinavigationcontroller composition

See, Uinavigationcontroller has Navigation bar, Navigation View, Navigation Toobar and other components.

Now let's create an example to see how to use the Uinavigationcontroller

3. Create a new project

Named Uinavigationcontrollerdemo, in order to better understand uinavigationcontroller, we choose the empty application template

4, Create a View Controller, named Rootviewcontroller: Select File--new--new File, the default check with the XIB for user interface.

Choose the correct location to create the complete, then the project three more files, respectively, RootViewController.h rootviewcontroller.m rootviewcontroller.xib file.

Open Rootviewcontroller.xib, add a button control, Change button to: Goto Secondview, prepare for jump

5. Open AppDelegate.hand add attributes to it:

[CPP]View Plaincopyprint?
    1. @property (Strong, nonatomic) Uinavigationcontroller *navcontroller;
@property (Strong, nonatomic) Uinavigationcontroller *navcontroller;

Add the following AppDelegate.h file code as follows:

[CPP]View Plaincopyprint?
    1. #import  <UIKit/UIKit.h>  
    2.   
    3. @class viewcontroller;  
    4.   
    5. @interface  AppDelegate : UIResponder < uiapplicationdelegate>  
    6.   
    7. @property   (strong,  nonatomic)  uiwindow *window;  
    8.   
    9. @property   ( strong, nonatomic)  viewcontroller *viewcontroller;  
    10.   
    11. @property   (strong, nonatomic)  UINavigationController *navController;   
    12.   
    13. @end   
#import <UIKit/UIKit.h> @class viewcontroller, @interface appdelegate:uiresponder <uiapplicationdelegate > @property (Strong, nonatomic) UIWindow *window; @property (Strong, nonatomic) Viewcontroller *viewcontroller;@ Property (Strong, Nonatomic) Uinavigationcontroller *navcontroller; @end

6. Create the Add Navcontroller,rootviewcontroller view in the Didfinishlaunchingwithoptions method of the APPDELEGATE.M file.

[CPP]View Plaincopyprint?
  1. -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
  2. {
  3. Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
  4. Rootviewcontroller *rootview = [[Rootviewcontroller alloc] init];
  5. Rootview.title = @"Root View";
  6. Self.navcontroller = [[Uinavigationcontroller alloc] init];
  7. [Self.navcontroller Pushviewcontroller:rootview Animated:yes];
  8. [Self.window AddSubview:self.navController.view];
  9. [Self.window makekeyandvisible];
  10. return YES;
  11. }
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{    Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];    Rootviewcontroller *rootview = [[Rootviewcontroller alloc] init];    Rootview.title = @ "Root View";        Self.navcontroller = [[Uinavigationcontroller alloc] init];    [Self.navcontroller Pushviewcontroller:rootview Animated:yes];    [Self.window AddSubview:self.navController.view];    [Self.window makekeyandvisible];    return YES;}

Name the Rootview titie Root view to identify the direct switching relationship of the view. Add the Rootview to the Navcontroller view stack with Pushviewcontroller.

7. Now the root view is added to complete

Look at the effect:

‘

There's no navigation bar. Only title.

8. Add Uibarbuttonitem

Bar Buttonitem about Uibarbuttonitem. We add all the left and right.

Add the following code in the ROOTVIEWCONTROLLER.M:

[CPP]View Plaincopyprint?
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. Uibarbuttonitem *leftbutton = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemaction Target:self Action: @selector (selectleftaction:)];
  5. Self.navigationItem.leftBarButtonItem = LeftButton;
  6. Uibarbuttonitem *rightbutton = [[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target: Self action: @selector (selectrightaction:)];
  7. Self.navigationItem.rightBarButtonItem = rightbutton;<p class="P1" >}</p>
-(void) viewdidload{    [Super Viewdidload];    Uibarbuttonitem *leftbutton = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemaction Target:self Action: @selector (selectleftaction:)];    Self.navigationItem.leftBarButtonItem = LeftButton;        Uibarbuttonitem *rightbutton = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemadd  Target:self Action: @selector (selectrightaction:)];    Self.navigationItem.rightBarButtonItem = Rightbutton;

}

This adds the Uibarbuttonitem, the effect is as follows:

Here are the highlights

Uibarbuttonitem *leftbutton = [[uibarbuttonitemalloc]initwithbarbuttonsystemitem: Uibarbuttonsystemitemactiontarget:selfAction:@selector(selectleftaction:)];

Uibarbuttonsystemitemaction style, this is the system comes with the button style, see, you do not have to experiment, you also know that you want to use the item, such as:

9. Implementation of events in response to Uibarbuttonitem

We are in action:@selector(selectleftaction:);

Action adds selectleftaction and Selectrightaction

Add code implementations in the ROOTVIEWCONTROLLER.M file:

[CPP]View Plaincopyprint?
  1. -(void) Selectleftaction: (ID) Sender
  2. {
  3. Uialertview *alter = [[Uialertview alloc] initwithtitle:@"hint" message:@"you clicked the navigation bar left button" Delegate:self  cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
  4. [Alter show];
  5. }
  6. -(void) Selectrightaction: (ID) Sender
  7. {
  8. Uialertview *alter = [[Uialertview alloc] initwithtitle:@"hint" message:@"you clicked the Navigation bar right button" delegate:self  cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
  9. [Alter show];
  10. }
-(void) Selectleftaction: (ID) sender{    uialertview *alter = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ " You clicked the navigation bar left button "Delegate:self  cancelbuttontitle:@" OK "otherbuttontitles:nil, nil";    [Alter show];} -(void) Selectrightaction: (ID) sender{    uialertview *alter = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ " You clicked the Navigation bar right button "Delegate:self  cancelbuttontitle:@" OK "otherbuttontitles:nil, nil";    [Alter show];}

So when you click on the left and right uibarbuttonitem, pop-up hints:


This article first to add Uibarbuttonitem, the next page to explain the jump and add Uisegmentedcontrol

http://blog.csdn.net/totogo2010/article/details/7681879

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.