[Transfer]ios Learning Uinavigationcontroller and use (ii) page switching and Segmentedcontroller

Source: Internet
Author: User

Reprint Address: http://blog.csdn.net/totogo2010/article/details/7682433

iOS learning Uinavigationcontroller and use (a) Add Uibarbuttonitem is the last chapter, we continue to talk about the important role of Uinavigationcontroller, page management and switching.

1, Rootview jump to Secondview

First we need a new view. Create a new Secondview, press and hold the command key and press N to pop up the new page, we create a new Secondview

2. Add Click events for button to achieve jump

Establishing a connection in Rootviewcontroller.xib and RootViewController.h files

Implement the code in ROOTVIEWCONTROLLER.M, alloc a secondviewcontroller, use Pushviewcontroller to Navigationcontroller, and

Secondviewcontroller This is the title of Secondview. title =@ "Second View"; By default, Titie returns the name of the button for the next page.

[CPP]View Plaincopy
    1. -(Ibaction) Gotosecondview: (ID) Sender {
    2. Secondviewcontroller *secondview = [[Secondviewcontroller alloc] init];
    3. [Self.navigationcontroller Pushviewcontroller:secondview Animated:yes];
    4. Secondview.title = @"Second View";
    5. }

This is the Click Gotosecondview button that appears

This is the Secondview.

3. Add Segmentedcontroller

How is this effect achieved in Nav bar?

This is Segmentedcontroller.

3.1 Add the following code to the ROOTVIEWCONTROLLER.M viewdidload:

[CPP]View Plaincopy
    1. Nsarray *array = [Nsarray arraywithobjects:@"chicken Wings", @"ribs", nil];
    2. Uisegmentedcontrol *segmentedcontroller = [[Uisegmentedcontrol alloc] initwithitems:array];
    3. Segmentedcontroller.segmentedcontrolstyle = Uisegmentedcontrolsegmentcenter;
    4. [Segmentedcontroller addtarget:self Action: @selector (segmentaction:) forcontrolevents:uicontroleventvaluechanged] ;
    5. Self.navigationItem.titleView = Segmentedcontroller;


3.2[segmentedcontroller addTarget:The Self action: implementation

[CPP]View Plaincopy
  1. -(void) Segmentaction: (ID) Sender
  2. {
  3. switch ([sender Selectedsegmentindex]) {
  4. Case 0:
  5. {
  6. Uialertview *alter = [[Uialertview alloc] initwithtitle:@"hint" message:@"you clicked on Chicken Wings" delegate:self  cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
  7. [Alter show];
  8. }
  9. Break ;
  10. Case 1:
  11. {
  12. Uialertview *alter = [[Uialertview alloc] initwithtitle:@"hint" message:@"you clicked on the ribs" delegate:self  cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
  13. [Alter show];
  14. }
  15. Break ;
  16. Default:
  17. Break ;
  18. }
  19. }

So you can respond to the chicken wings and ribs button.

4. Custom Backbarbuttonitem

The top left corner of the return to the upper view of the Barbuttonitem name is the title of the parent directory, if the title or the name of the button is appropriate to do? We can define it ourselves.

The code is as follows:

[CPP]View Plaincopy
    1. Uibarbuttonitem *backbutton = [[Uibarbuttonitem alloc] initwithtitle:@"root View" Style:uibarbuttonitemstyledone target  : Nil Action:nil];
    2. Self.navigationItem.backBarBu

Effect:

6. Custom Title

Uinavigationcontroller title can be used instead of the view, such as with UIButton uilable, the following I use UIButton.

In SECONDVIEWCONTROLLER.M, add the following as follows.

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. -(void) Viewdidload
    2. {
    3. [Super Viewdidload];
    4. UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect];
    5. [Button Settitle: @"custom title" Forstate:uicontrolstatenormal];
    6. [Button SizeToFit];
    7. Self.navigationItem.titleView = button;}

Run program, goto Secondview, run effect



The next document tells how Navigation's toobar are displayed and how to define them.

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.