The above is the effect of the navigation bar, which is widely used in projects and needs to be mastered.
Create a project and select "empty application". The project name is navigationcontrollertest.
Create a new uiviewcontroller view named homeviewconroller
Modify source code of appdeledate. h and appdolegate. m
Idea: Push home to "navigationcontroller" and add navigationcontroller. View to window.
// Appdelegate. m
# Import <uikit/uikit. h>
@ Interface appdelegate: uiresponder <uiapplicationdelegate>
{
Uinavigationcontroller * navigationcontroller;
}
@ Property (strong, nonatomic) uiwindow * window;
@ End
// Appdelegate. m
# Import "appdelegate. H"
# Import "homeviewcontroller. H"
@ Implementation appdelegate
@ Synthesize window = _ window;
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
Self. Window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease];
Self. Window. backgroundcolor = [uicolor whitecolor];
Navigationcontroller = [[uinavigationcontroller alloc] init];
Homeviewcontroller * home = [[homeviewcontroller alloc] init];
Home. Title = @ "Memorandum ";
[Navigationcontroller pushviewcontroller: Home animated: No];
[Self. Window addsubview: navigationcontroller. View];
[Home release];
[Self. Window makekeyandvisible];
Return yes;
}
-(Void) dealloc
{
[Navigationcontroller release];
[_ Window release];
[Super dealloc];
}
@ End
The effect is as follows:
The above is only a page, and a new uiviewcontroller View "secondviewcontroller" is created below, so that the project can be switched between the two pages.
Add the button "Enter second view" on homeviewcontroller. XIB"
Add in homeviewcontroller
-(Ibaction) displaysecondview :( ID) sender method.
-(Void) displaysecondview :( ID) sender
{
Secondviewcontroller * secondviewconroller = [[secondviewcontroller alloc] init];
// Ask the view about its navigation controller, because we have added home to navigationcontroller in appdelegate. M,
// So here we ask the home navigation controller to return the previous navigationcontroller pointer, and if not, return null
[Self. navigationcontroller pushviewcontroller: secondviewconroller animated: Yes];
Secondviewconroller. Title = @ "second view ";
[Secondviewconroller release];
}
The effect is as follows:
When you switch to sencondviewcontroller, the navigation bar automatically displays the button for returning the first view.
Implement the left and right buttons in the navigation bar.
This task is mainly implemented by uinavigationitem in uinavigationcontroller.
Add uibarbuttonitem on uinavigationitem.
Modify the code of-(void) viewdidload in homeviewcontroller. M:
-(Void) viewdidload
{
[Super viewdidload];
Uibarbuttonitem * leftbarbtn = [[uibarbuttonitem alloc] initwithtitle: @ "Touch" style: uibarbuttonitemstylebordered target: Self action: @ selector (leftbarbtnclicked :)];
Self. navigationitem. leftbarbuttonitem = leftbarbtn;
[Leftbarbtn release];}
-(Void) leftbarbtnclicked :( ID) sender
{
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "prompt" message: @ "The barbutton on the left is clicked! "Delegate: Self cancelbuttontitle: @" OK "otherbuttontitles: Nil];
[Alert show];
[Alert release];
}
The effect is as follows:
There are also some button and action. The system has already defined the action of the edit and done buttons for us to implement it.
The following is the same as the above principle. Let's look at the Code. The Code is better than anything else.
The Code is as follows:
-(Void) viewdidload
{
[Super viewdidload];
Uibarbuttonitem * leftbarbtn = [[uibarbuttonitem alloc] initwithtitle: @ "Touch" style: uibarbuttonitemstylebordered target: Self action: @ selector (leftbarbtnclicked :)];
Self. navigationitem. leftbarbuttonitem = leftbarbtn;
[Leftbarbtn release];
Uibarbuttonitem * addbarbtn = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemadd target: Self action: @ selector (addbarbtnclicked :)];
Self. navigationitem. rightbarbuttonitem = addbarbtn;
[Addbarbtn release];
Self. navigationitem. rightbarbuttonitem = self. editbuttonitem;
Nsarray * segmentbuttons = [nsarray arraywithobjects: @ "ASCENDING", @ "descending", nil];
Uisegmentedcontrol * segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems: segmentbuttons];
Segmentedcontrol. segmentedcontrolstyle = uisegmentedcontrolstylebar;
[Segmentedcontrol addtarget: Self action: @ selector (segmentaction :) forcontrolevents: uicontroleventvaluechanged];
Self. navigationitem. titleview = segmentedcontrol;
[Segmentedcontrol release];
// After navigating to another view, modify the display text of the returned button. The default value is the navigation title of the current view, for example, "Memorandum" uibarbuttonitem * backbarbtn = [[uibarbuttonitem alloc] initwithtitle: @ "return" style: uibarbuttonitemstyleplain target: Nil action: Nil];
Self. navigationitem. backbarbuttonitem = backbarbtn;
[Backbarbtn release];
}
-(Void) leftbarbtnclicked :( ID) sender
{
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "prompt" message: @ "The barbutton on the left is clicked! "Delegate: Self cancelbuttontitle: @" OK "otherbuttontitles: Nil];
[Alert show];
[Alert release];
}
-(Void) addbarbtnclicked :( ID) sender
{
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "prompt" message: @ "addbarbutton on the right is clicked! "Delegate: Self cancelbuttontitle: @" OK "otherbuttontitles: Nil];
[Alert show];
[Alert release];
}
-(Void) setediting :( bool) Editing animated :( bool) animated
{
[Super setediting: editing animated: animated];
If (editing)
{
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "prompt" message: @ "edit" delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil];
[Alert show];
[Alert release];
}
Else
{
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "prompt" message: @ "done" delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil];
[Alert show];
[Alert release];
}
}
-(Void) segmentaction :( ID) sender
{
Switch ([Sender selectedsegmentindex])
{
Case 0:
{
Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "prompt" message: @ "ASCENDING" delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil];
[Alert show];
[Alert release];
Break;
}
Case 1:
{
Uialertview * alert1 = [[uialertview alloc] initwithtitle: @ "prompt" message: @ "descending" delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: Nil];
[Alert1 show];
[Alert1 release];
Break;
}
Default:
Break;
}}