1. How to Use UINavigationController
UINavigationController can be translated into a navigation controller, which is often used in iOS.
Let's see how it works:
The following figure shows the flow of the navigation controller. The root view is on the left. When you click the General item, the General view slides into the screen. When you continue to click the Auto-Lock item, the Auto-Lock view slides into the screen. Correspondingly, in object management, the navigation controller uses the navigation stack. The Root View Controller is at the bottom layer of the stack. The General view controller and Auto-Lock View Controller are used to import the Controller to the stack. You can call pushViewControllerAnimated to push the View Controller to the top of the stack, or call popViewControllerAnimated to bring the View Controller to the stack.
From the official website of apple.
2. Structure of UINavigationController
See, UINavigationController consists of Navigation bar, Navigation View, and Navigation toobar.
Now let's create an example to see how to use UINavigationController
3. Create a project
Name it UINavigationControllerDemo. To better understand UINavigationController, we select the Empty Application template.
4. Create a View Controller named RootViewController. Select File -- New File, and check With XIB for user interface by default.
Select the correct location to complete the creation. In this case, three files are added to the project, namely RootViewController. h RootViewController. m RootViewController. xib.
Open RootViewController. xib and add a Button control. Change the Button to Goto SecondView to prepare for the jump.
5. Open AppDelegate. h and add the following attributes to it:
@property (strong, nonatomic) UINavigationController *navController;
The code for the added AppDelegate. h file is 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 the navController and RootViewController views in the didfinishlaunchingwitexceptions method of the AppDelegate. m file.
- (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 titie of the rootView as the Root View to identify the direct switching relationship of the View. Use pushViewController to add rootView to the view stack of navController.
7. Now the Root View is added
See the results:
'
There is no Navigation bar yet. Only title.
8. Add UIBarButtonItem
Bar ButtonItem: About UIBarButtonItem. We can add both left and right.
Add the following code to RootViewController. m:
- (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;
}
UIBarButtonItem is added as follows:
Here we will focus on
UIBarButtonItem * leftButton = [[UIBarButtonItemalloc] initWithBarButtonSystemItem: UIBarButtonSystemItemActiontarget: selfaction: @ selector (selectLeftAction :)];
The UIBarButtonSystemItemAction style is the button style that comes with the system. You don't need to experiment one by one. You also know the item you want to use, for example:
9. Implementation of responding to UIBarButtonItem events
In action: @ selector (selectLeftAction :);
SelectLeftAction and selectRightAction are added to action.
Add code implementation to the RootViewController. m file:
-(Void) selectLeftAction :( id) sender {UIAlertView * alter = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "you have clicked the left button of the navigation bar" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alter show] ;}- (void) selectRightAction :( id) sender {UIAlertView * alter = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "you clicked the right button in the navigation bar" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alter show];}
In this way, when you click the Left or Right UIBarButtonItem, a prompt is displayed:
Add UIBarButtonItem in this article. Next, go to the page and add UISegmentedControl.
Next article: understanding and using UINavigationController for iOS (2) page switching and segmentedController
Example code: https://github.com/schelling/YcDemo
Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you.