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
- -(Ibaction) Gotosecondview: (ID) Sender {
- Secondviewcontroller *secondview = [[Secondviewcontroller alloc] init];
- [Self.navigationcontroller Pushviewcontroller:secondview Animated:yes];
- Secondview.title = @"Second View";
- }
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
- Nsarray *array = [Nsarray arraywithobjects:@"chicken Wings", @"ribs", nil];
- Uisegmentedcontrol *segmentedcontroller = [[Uisegmentedcontrol alloc] initwithitems:array];
- Segmentedcontroller.segmentedcontrolstyle = Uisegmentedcontrolsegmentcenter;
- [Segmentedcontroller addtarget:self Action: @selector (segmentaction:) forcontrolevents:uicontroleventvaluechanged] ;
- Self.navigationItem.titleView = Segmentedcontroller;
3.2[segmentedcontroller addTarget:The Self action: implementation
[CPP]View Plaincopy
- -(void) Segmentaction: (ID) Sender
- {
- switch ([sender Selectedsegmentindex]) {
- Case 0:
- {
- Uialertview *alter = [[Uialertview alloc] initwithtitle:@"hint" message:@"you clicked on Chicken Wings" delegate:self cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
- [Alter show];
- }
- Break ;
- Case 1:
- {
- Uialertview *alter = [[Uialertview alloc] initwithtitle:@"hint" message:@"you clicked on the ribs" delegate:self cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
- [Alter show];
- }
- Break ;
- Default:
- Break ;
- }
- }
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
- Uibarbuttonitem *backbutton = [[Uibarbuttonitem alloc] initwithtitle:@"root View" Style:uibarbuttonitemstyledone target : Nil Action:nil];
- 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" >
- -(void) Viewdidload
- {
- [Super Viewdidload];
- UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect];
- [Button Settitle: @"custom title" Forstate:uicontrolstatenormal];
- [Button SizeToFit];
- 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.