Add tutorials for navigation bar button Uibarbuttonitem in IOS application development _ios

Source: Internet
Author: User
Tags uikit

1. How to use the Uinavigationcontroller navigation controller
Uinavigationcontroller can be translated as a navigation controller, 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, and when the user clicks on the general item, the general view slides into the screen, and the Auto-lock view slides into the screen when the user clicks on 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 onto the top of the stack, or call popviewcontrolleranimated: method to control the view

2, the structure of the uinavigationcontroller composition
Look at the picture below, Uinavigationcontroller has navigation bar, navigation View, navigation toobar and so on.

Now let's build an example to see how to use the Uinavigationcontroller
3. Create a new project
Named Uinavigationcontrollerdemo, in order to better understand uinavigationcontroller, we chose empty application template

4, create a view Controller, named Rootviewcontroller: Select File--new--new File, the default hook with the XIB for user interface.

Select the correct location to create the complete, then the project has three more files, respectively, RootViewController.h rootviewcontroller.m rootviewcontroller.xib file.
Open Rootviewcontroller.xib, add a button control, Change button to: Goto Secondview, ready for jump

5. Open AppDelegate.h and add attributes to it:

Copy Code code as follows:

@property (Strong, nonatomic) Uinavigationcontroller *navcontroller;

Add the following AppDelegate.h file code as follows:
Copy Code code as follows:

#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 Add Navcontroller,rootviewcontroller view in the Didfinishlaunchingwithoptions method of APPDELEGATE.M file.
Copy Code code as follows:

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

The Rootview Titie is named Root view to identify the direct switching relationship of the view. Use Pushviewcontroller to add Rootview to the Navcontroller view stack.
7, now root view add complete
Look at the effect:

There's no navigation bar yet. Only title.
8, add Uibarbuttonitem
Bar Buttonitem about Uibarbuttonitem. We add all the right and left sides.
Add the code in the ROOTVIEWCONTROLLER.M as follows:

Copy Code code as follows:

-(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;<p class= "P1"}

This adds the Uibarbuttonitem, and the effect is as follows:

Here are the highlights

Copy Code code as follows:

Uibarbuttonitem *leftbutton = [[Uibarbuttonitemalloc]initwithbarbuttonsystemitem: Uibarbuttonsystemitemactiontarget:selfaction: @selector (selectleftaction:)];


Uibarbuttonsystemitemaction style, this is the system with the button style, look at the picture below, you do not have to test each, you also know want to use the item, the following figure:

9, the response to the implementation of Uibarbuttonitem events
We are in action: @selector (selectleftaction:);
Action adds selectleftaction and Selectrightaction
To add code implementations to the ROOTVIEWCONTROLLER.M file:

Copy Code code as follows:

-(void) Selectleftaction: (ID) sender
{
Uialertview *alter = [[Uialertview alloc] initwithtitle:@ "prompts" message:@ "you clicked the navigation bar left button" Delegate:self cancelbuttontitle:@ " Determine "otherbuttontitles:nil, nil];
[Alter show];
}

-(void) Selectrightaction: (ID) sender
{
Uialertview *alter = [[Uialertview alloc] initwithtitle:@ "prompts" message:@ "you clicked the Navigation bar right button" Delegate:self cancelbuttontitle:@ " Determine "otherbuttontitles:nil, nil];
[Alter show];
}

So when you click on the Uibarbuttonitem, pop-up prompts:


A simple example of a two-button switch

The background of the following code example is that there is an edit button on the right side of the navigation bar and a Back button and an Add button on the left. Code implementation of the button switch/Hide function is: Click on the Edti button, the Back button hidden, while displaying the Add button. When the user finishes editing, the Back button is displayed to hide the Add button. This feature is used in many applications, and proper concealment of unwanted buttons is meaningful to keep the interface simple and to guide user actions.

Copy Code code as follows:

-(void) viewdidload {
[super viewdidload];
Self.navigationItem.rightBarButtonItem = Self.editbuttonitem;
}
-(void) setediting: (BOOL) editing animated: (bool) animated {
 
    [Super seted Iting:editing animated:animated];
 
//Don T show the "Back button" while editing.
[Self.navigationitem sethidesbackbutton:editing Animated:yes];
 
if (editing) {
    self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc ] Initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target:self Action: @selector (INSERTME)] autorelease];
}else {
    self.navigationItem.leftBarButtonItem = nil;
//self.navigationitem.leftbarbuttonitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemcancel target:self Action: @selector (Backbutton)] autorelease];
 }
}

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.