UINavigationController for iOS (1) adding UIBarButtonItem is the previous article. Let's talk about the important role of UINavigationController, Page Management and switching.
1. RootView jump to SecondView
First, we need a new View. Create a SecondView, press the Command key, and then press N. The new page is displayed.
2. Add a click event for the Button to redirect
Connect to RootViewController. xib and RootViewController. h file
Implement code in RootViewController. m, alloc A SecondViewController, use pushViewController to go to navigationController, and
SecondViewController this is the title of secondView. title = @ "Second View"; by default, titie is the name of the button returned for the next page.
- (IBAction)gotoSecondView:(id)sender { SecondViewController *secondView = [[SecondViewController alloc] init]; [self.navigationController pushViewController:secondView animated:YES]; secondView.title = @"Second View";}
Click GotoSecondView.
This is SecondView.
3. Add segmentedController
How is the effect of nav bar achieved?
This is segmentedController.
3.1 Add the following code in viewDidLoad of RootViewController. m:
NSArray * array = [NSArray arrayWithObjects: @ "Chicken Wings", @ "", nil]; UISegmentedControl * segmentedController = [[UISegmentedControl alloc] initWithItems: array]; segmentedController. segmentedControlStyle = UISegmentedControlSegmentCenter; [segmentedController addTarget: self action: @ selector (segmentAction :) forControlEvents: UIControlEventValueChanged]; self. navigationItem. titleView = segmentedController;
3.2 [Implementation of segmentedController addTarget: selfaction:
-(Void) segmentAction :( id) sender {switch ([sender selectedSegmentIndex]) {case 0: {UIAlertView * alter = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "you clicked Chicken Wings" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alter show];} break; case 1: {UIAlertView * alter = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alter show];} break; default: break ;}}
In this way, you can respond to the chicken wings and ribs buttons.
4. Customize backBarButtonItem
In the upper left corner, the barButtonitem of the upper-level View is returned as the Title of the upper-level directory. What should I do if the title is the right button name? We can define it ourselves
The Code is as follows:
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithTitle: @ "Root View" style: UIBarButtonItemStyleDone target: nil action: nil]; self. navigationItem. backBarBu
Effect:
6. Custom title
The title of UINavigationController can be replaced by another view, for example, UIButton UILable. Below I will use UIButton.
Add the following in SecondViewController. m.
-(Void) viewDidLoad {[super viewDidLoad]; UIButton * button = [UIButton buttonWithType: Custom]; [button setTitle: @ "Custom title" forState: UIControlStateNormal]; [button sizeToFit]; self. navigationItem. titleView = button ;}
Run the program, goto secondView, and the running effect
The next article describes how to display and define the Toobar of Navigation.
Part II:
UINavigationController for iOS (3) ToolBar
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.