UINavigationController for iOS (2) page switching and segmentedController

Source: Internet
Author: User

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.

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.