IOS Study Notes (5) uinavigationcontroller uitabbarcontroller

Source: Internet
Author: User

Uinavigationcontroller for navigation

In the. h file in APP delegate

@ Property (nonatomic, strong) uinavigationcontroller * nav;

In the. M file

@ Synthesize nav;

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {

Self. Nav = [[uinavigationcontroller alloc] initwithrootviewcontroller: Self. rootviewcontroller];

[Self. Window addsubview: Self. Nav. View];

Return yes;

}

On the viewcontroller page

-(Void) viewdidload {

Self. Title = @ "Hello World ";

}

Deeper:

5 seconds after the first View Controller appears on the screen, drag the second view controller to its top:

First, add the second view controller to the first view controller:

# Import "secondviewcontroller"

-(Void) pushsecondcontroller {

Secondviewcontroller * secondcontroller = [[secondviewcontroller alloc] initwithnibname: Nil Bundle: NULL];

[Self. navigationcontroller pushviewcontroller: secondcontroller
Animated: Yes];

}

-(Void) viewdidappear :( bool) paramanimated {

[Super viewdidappear: paramanimated];

[Self defined mselector: @ selecter (pushsecondcontroller) withobject: Nil afterdelay: 5.0f];

}

Since it can be dragged in, it can be removed:

-(Void) Goback {

[Self. navigationcontroller popviewcontrolleranimated: Yes];

}

-(Void) didappear :( bool) paramanimated {

[Super viewdidappear: paramanimated];

[Self defined mselector: @ selector (Goback) withobject: Nil afterdelay: 5.0f];

}

Adjust the sequence of navigation controllers in view Controllers

Use
The viewcontrollers attribute of the uinavigationcontroller class obtains and modifies the order of view controllers associated with the navigation controller:

-(Void) Goback {

Nsarray * currentcontrollers = self. navigationcontroller. viewcontrollers;

Nsmutablearray * newcontrollers = [nsmutablearray arraywitharray: currentcontrollers];

[Newcontrollers removelastobject];

Self. navigationcontroller. viewcontrollers = newcontrollers;

// Animation complete

// [Self. navigationcontroller setviewcontrollers: newcontrollers animated: Yes];

}

To push the last view controller from the hierarchy of the navigation controller associated with the current view controller, you can call this method in any view controller.

Display an image in the navigation bar

To replace text with an image in the title of the current view of the navigation Controller

-(Void) viewdidload {

[Super viewdidload];

Self. View. backgroundcolor = [uicolor whitecolor];

Uiimageview * imageview = [[uiimageview alloc] initwithframe: cgrectmake (0.0f, 0.0f, 100366f, 40366f );

Imageview. contentmode = uiviewcontentmodescaleaspectfit;

Uiimage * image = [uiimage imagenamed: @ "fullsizelogo.png"];

[Imageview setimage: Image];

Self. navigationitem. titleview = imageview;

}

Download the above demo

Use the uibarbuttonitem class to add a button in the navigation bar

Before creating a navigation button: Create a uibarbuttonitem class instance and add a button to the navigation bar using the navigationitem attribute of the view space. The navigationitem attribute allows us to set this navigation bar. This attribute has two attributes: rightbarbuttonitem and leftbarbuttonitem. Both attributes belong to the uibarbuttonitem class.

-(Void) viewdidload {

[Super viewdidload];

Self. View. backgroundcolor = [uicolor whitecolor];

Self. Title = @ "Hello World ";

Self. navigationitem. rightbarbuttonitem = [[uibarbuttonitem alloc] initwithtitle: @ "right" style: uibarbuttonitemstyleplain target: Self action: @ selector (adjust mright :)];

}

-(Void) initialize mright :( ID) right {

Nslog (@ "clicked rightbutton ");

}

System button Initialization Method 1:

Initwithbarbuttonsystemitem: Target: Action: Initialization Method

Self. navigationitem. leftbarbuttonitem = [[uibarbuttonitem alloc] initwithbartbuttonsystemitem: uibarbuttonsystemitemadd target: Self action: @ selector (wrong mright :)];

The initialization button of the navigation button:

Typedef Enum {

Uibarbuttonsystemitem done/cancel/edit/save/Add/flexiblespace/fixedspace/compose/reply/Action/organize/bookmarks/search/refresh/Stop/camera/trash/play/pause/rewind /fastforward/Undo/Redo/pagecurl/

} Uibarbuttonsystemitem;

Method 2:

Initwithcustomview: method (you can add uiswitch)

-(Void) viewdidload {

[Super viewdidload];

Self. View. backgroundcolor = [uicolor whitecolor];

Self. Title = @ "Hello World ";

Uiswitch * myswitch = [uiswitch alloc] init];

Myswitch. On = yes;

[Myswitch addtarget: Self action: @ selector (switchchanged :) forcontrolevents: uicontroleventvaluechanged];

Self. navigationitem. rightbarbuttonitem = [[uibarbuttonitem alloc] initwithcustomview: myswitch];

}

Yes. Test it.

So let's make a demo of the up and down arrows.

-(Void) viewdidload {

[Super viewdidload];

Self. View. backgroundcolor = [uicolor whitecolor];

Self. Title = @ "Hello World ";

Nsarray * items = [[nsarray alloc] initwithobjects: [uiimage imagenamed: @ "uparrow.png"], [uiimage imagenamed: @ "downarrow.png"], nil];

Uisegmentedcontrol * segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems: items];

Segmentedcontrol. segmentedcontrolstyle = uisegmentedcontrolstylebar;

Segmentedcontrol. Momentary = yes;

[Segmentedcontrol addtarget: Self action: @ selector (segmentedcontroltapped :) forcontrolevents: uicontroleventvaluechanged];

Self. navigationitem. rightbarbuttonitem = [[uibarbuttonitem alloc] initwithcustomview: segmentedcontrol];

// Set the animation

// Uibarbuttonitem * rightbarbutton = [[uibarbuttonitem alloc] initwithcustomview: segmentedcontrol];

[Self. navigationitem setrightbarbuttonitem: rightbarbutton animated: Yes];

}

Use uitabbarcontroller to display multi-view Controllers

In appdelegate. h

@ Class firstviewcontroller;

@ Class secondviewcontroller;

@ Property (nonatomic, strong) firstviewcontroller * firstviewcontroller;

@ Property (non atomic, strong) secondviewcontroller * secondviewcontroller;

@ Property (non atomic, strong) uitablebarcontroller * tabbarcontroller;

In the appdelegate. M file

@ Synthesize firstviewcontroller, secondviewcontroller, tabbarcontroller;

Self. firstviewcontroller = [[firstviewcontroller alloc] initwithnibname: Nil Bundle: NULL];

Self. secondviewcontroller = [[secondviewcontroller alloc] initwithnibname: Nil Bundle: NULL];

Nsarray * twoviewcontrollers = [[nsarray alloc] initwithobjects: Self. firstviewcontroller, self. secondviewcontroller, nil];

Self. tabbarcontroller = [[uitabbarcontroller alloc] init];

[Self. tabbarcontroller setviewcontrollers: twoviewcontrollers];

When you run the program, there is no navigation. What should we do? Proceed to the next step.

In appdelegate. H

@ Proterty (non atomic, strong) uinavigationcontroller * nav;

In the appdelegate. M file

@ Synthesize nav;

Self. firstnavigationcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller: Self. firstviewcontroller];

Self. secondnavigationcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller: Self. secondviewcontroller];

Nsarray * twonavigationcontroller = [[nsarray alloc] initwithobjects: Self. firstnavigationcontroller, self. secondnavigationcontroller, nil];

Self. tabbarcontroller = [[uitabbarcontroller alloc] init];

[Self. tabbarcontroller setviewcontrollers: twonavigationcontroller];

Self. Window addsubview: Self. tabbarcontroller. View];

Tabbaritem attributes:

Firstviewcontroller. m

-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil {

Self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil];

If (self! = Nil ){

Self. Title = @ "first ";

Self. tabbaritem. Image = [uiimage image named: @ "firsttb.png"];

}

Return self;

}

Secondviewcontroller is the same as above

Download the following demo
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.