Uikit Frame (11) Navigation controller Uinavigationcontroller

Source: Internet
Author: User
Tags uikit

Introduces a controller that is very important in multi-controller management Uinavigationcontroller

It is responsible for managing multiple controllers, enabling easy switching between controllers

Such as: Settings on the iOS system

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/9E/wKioL1bT6Y7gOaaXAAC7Ewouv1A120.png "title=" screen shot 2016-02-29 pm 2.45.38.png "alt=" Wkiol1bt6y7goaaxaac7ewouv1a120.png "/>


The parent class is uiviewcontroller, but its function is not to manage the view, but to manage multiple controllers


Controller-Stack Management:

When switching, put a controller into the stack, when returned, stack top controller out of the stack

The current display is always the view of the top controller, the stack structure is as follows:

@property (nonatomic, copy) Nsarray *viewcontrollers//Managed all Controllers @property (Nonatomic, ReadOnly, retain) Uiviewcontrolle R *topviewcontroller//controller currently displayed


    • Code using the Navigation controller

Navigation Controller creation:

-(Instancetype) Initwithrootviewcontroller: (Uiviewcontroller *) Rootviewcontroller

Rootviewcontroller parameter: Root controller, the first page to manage


Jump action:

Pushviewcontroller method using the navigation controller:

-(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated

In other words, the performer of the Jump action is the navigation controller, not the source controller

The source controller can access the navigation controller through its properties:

@property (Nonatomic, ReadOnly, retain) Uinavigationcontroller*navigationcontroller

such as a jump action:

Amviewcontroller *VC = [[Amviewcontrollertwo alloc] init]; [Self.navigationcontroller PUSHVIEWCONTROLLER:VC Animated:yes];

Return action:

On the navigation bar of the non-root controller, there is a return button, which can be automatically returned by clicking this button.

You can also navigate the controller's method by returning the code:

-(Uiviewcontroller *) popviewcontrolleranimated: (BOOL) animated//Return a controller-(Nsarray *) Poptoviewcontroller: ( Uiviewcontroller *) Viewcontroller animated: (BOOL) animated//return to the specified controller-(Nsarray *) poptorootviewcontrolleranimated: ( BOOL) animated//return to the root controller




    • Storyboard using the navigation controller

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7C/9E/wKioL1bT6s3SqmfTAACaPXJo9qo871.png "title=" screen shot 2016-02-29 pm 2.51.07.png "alt=" Wkiol1bt6s3sqmftaacapxjo9qo871.png "/>

Remove the storyboard default controller, add Uinavigationcontroller, and set it to the initial controller.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7C/9E/wKioL1bT6vThL6FyAAA9u2LI0QM416.png "title=" screen shot 2016-02-29 pm 2.51.44.png "alt=" Wkiol1bt6vthl6fyaaa9u2li0qm416.png "/>

Add a controller to connect the root view controller properties of the navigation controllers

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/9E/wKioL1bT6wvhavSvAAD7-hGSf-s774.png "title=" screen shot 2016-02-29 pm 2.52.09.png "alt=" Wkiol1bt6wvhavsvaad7-hgsf-s774.png "/>

The jump between the controller can be directly connected, select Show

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7C/A0/wKiom1bT6qygMBnWAACGaW_0D1g966.png "title=" screen shot 2016-02-29 pm 2.52.34.png "alt=" Wkiom1bt6qygmbnwaacgaw_0d1g966.png "/>

also supports auto-segue and manual segue

Transferring data between controllers is the same as modal mode switching


Add a button on the navigation bar

The button type on the navigation bar is Uibarbuttonitem

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7C/9E/wKioL1bT60zxEIevAADh_BYn6j8173.png "title=" screen shot 2016-02-29 pm 2.53.14.png "alt=" Wkiol1bt60zxeievaadh_byn6j8173.png "/>

    • Navigation bar

Navigation Controller In addition to managing multiple controllers, there is a navigation bar, related properties:

@property (nonatomic, readonly) Uinavigationbar *navigationbar//navigation bar @property (nonatomic, getter= Isnavigationbarhidden) BOOL Navigationbarhidden//navigation bar Hide Enable


The data on the navigation bar is managed by the Uinavigationitem object, with the following properties:

@property (nonatomic, copy) NSString *title//Middle title @property (nonatomic, retain) UIView *titleview//Middle View @property ( Nonatomic, retain) Uibarbuttonitem *backbarbuttonitem//Back button on the left @property (nonatomic, retain) Uibarbuttonitem * Leftbarbuttonitem//Left-Hand custom button @property (nonatomic, copy) Nsarray *leftbarbuttonitems @property (nonatomic, retain) Uibarbuttonitem *rightbarbuttonitem//Right-custom button @property (nonatomic, copy) Nsarray *rightbarbuttonitems

The navigation bar Object Uinavigationbar manages how the data is displayed and the navigation controller manages multiple controllers similar to the structure of the stack:

@property (nonatomic, copy) Nsarray *items@property (nonatomic, ReadOnly, retain) Uinavigationitem *topitem

However, you do not usually use the navigation bar object directly to set the displayed data

Uiviewcontroller contains a Navigationitem property:

@property (Nonatomic, ReadOnly, retain) Uinavigationitem *navigationitem

With this property, the navigation bar data of the current page can be set directly


    • Tool bar

The navigation controller also manages a toolbar, which is hidden by default

@property (nonatomic, ReadOnly) Uitoolbar *toolbar-(void) Settoolbarhidden: (BOOL) hidden animated: (BOOL) Animated@property (nonatomic, Getter=istoolbarhidden) BOOL Toolbarhidde

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7C/9F/wKioL1bT7TzBliV8AAAwYbDLhG8487.png "title=" screen shot 2016-02-29 pm 3.01.21.png "alt=" Wkiol1bt7tzbliv8aaawybdlhg8487.png "/>

Toolbar Uitoolbar managed by navigation bar Uinavigationbar is managed in a similar way

Uiviewcontroller contains a Tabbaritem property:

@property (Nonatomic, ReadOnly, retain) Uinavigationitem *navigationitem

With this property, the toolbar data of the current page can be set directly

@property (nonatomic, retain) Uitabbaritem *tabbaritem



    • Navigation Controller sub-class

The benefits of defining a navigation controller subclass are:

To have all navigation controllers in the project follow a uniform set of behaviors, all navigation controllers should be a subclass of the custom Uinavigationcontroller, setting these uniform behaviors in subclasses.


Uinavigationbar setting a background image

-(void) SetBackgroundImage: (UIImage *) backgroundimage forbarmetrics: (uibarmetrics) Barmetrics


Uinavigationbar Setting Text properties

@property (nonatomic, copy) Nsdictionary *titletextattributes


Some controls follow the <UIAppearance> protocol and can return global objects through the appearance method in the Protocol

+ (Instancetype) appearance

Global objects enable you to set some global features

Set the tintcolor of the navigation bar

[Uinavigationbar appearance].tintcolor = [Uicolor Whitecolor];

Set the font for a navigation bar button

[Uibarbuttonitem appearance] settitletextattributes:attrdict Forstate:uicontrolstatenormal];


Global property settings, execute once

Placing the code in the Viewdidload method of the navigation controller, the global property setting executes multiple times when multiple navigation controllers appear.

A scheme that guarantees that a global property is only executed once is added to the Initialize method


You can also use a class to manage the appearance settings of all Uikit objects to enable the app skin to switch





This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746013

Uikit Frame (11) Navigation controller Uinavigationcontroller

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.