Basic tutorials for navigation controllers in IOS development _ios

Source: Internet
Author: User
Tags reserved

A simple introduction to multiple controllers and navigation controllers
One, multiple controllers

An iOS app is rarely made up of only one controller, unless the app is incredibly simple. When there are multiple controllers in the app, we need to manage these controllers

When you have multiple view, you can use a large view to manage 1 or more small view, the controller is the same, with 1 controllers to manage other multiple controllers

For example, use a controller A to manage 3 controllers B, C, D. Controller A is called the "parent controller" of Controller B, C, D, and Controller B, C, and D is known as Controller A "sub controller"

For easy management of the controller, iOS offers 2 more specific controllers

    • Uinavigationcontroller
    • Uitabbarcontroller

Second, the navigation controller

With Uinavigationcontroller, you can easily manage multiple controllers and easily switch between controllers, which is typically the "setup" application that comes with the system.

As shown in figure:

Third, the use of uinavigationcontroller steps

(1) Initialization of Uinavigationcontroller

(2) Setting the Rootviewcontroller of UIWindow as Uinavigationcontroller

(3) Add the corresponding number of sub controller by push method according to the specific situation

Copy Code code as follows:



#import "YYAppDelegate.h"


#import "YYOneViewController.h"

@implementation Yyappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Self.window.backgroundColor = [Uicolor Whitecolor];

1. Create a navigation controller
Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]init];
2. Set the root view of the Navigation Controller as window
Self.window.rootviewcontroller=nav;

   
   //3. Add a child controller to the navigation controller
   //create some controllers
     Uiviewcontroller *c1=[[uiviewcontroller Alloc]init];
   //Set C1 view color for this controller
    C1.view.backgroundcolor=[uicolor Redcolor];
   
    uiviewcontroller *c2=[[uiviewcontroller alloc]init];
     C2.view.backgroundcolor=[uicolor Purplecolor];
   
    uiviewcontroller *c3=[[uiviewcontroller alloc]init];
     C3.view.backgroundcolor=[uicolor Browncolor];
   
//Add these controllers to the navigation controller
    [nav pushviewcontroller:c1 animated:yes];
& nbsp;   [nav pushviewcontroller:c2 animated:yes];
    [nav pushviewcontroller:c3 animated:yes];
   
    [Self.window makekeyandvisible];
    return YES
}




Running the simulator, you can see a simple three-child controller that manages the page.

But now we have only one interface, we do not need to create a one-time three controllers here waiting.

Requirements: Create three sub controllers, put a button on the interface of each child controller view, click to jump to the next interface.

Implementation (complete three pages through the button for a simple jump):

Description: This is where the first child controller creation code is written in the proxy method.

YYAPPDELEGATE.M File Code

Copy Code code as follows:



//


Yyappdelegate.m


01-Use of the navigation controller 1


//


Created by Apple on 14-6-4.


Copyright (c) 2014 itcase. All rights reserved.


//

#import "YYAppDelegate.h"
#import "YYOneViewController.h"

@implementation Yyappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Self.window.backgroundColor = [Uicolor Whitecolor];

1. Create a navigation controller
Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]init];
2. Set the root view of the Navigation Controller as window
Self.window.rootviewcontroller=nav;




3. Add the child controller to the navigation controller


Yyoneviewcontroller *one=[[yyoneviewcontroller Alloc]init];


[Nav Pushviewcontroller:one Animated:yes];





[Self.window makekeyandvisible];


return YES;








Create some controllers


Uiviewcontroller *c1=[[uiviewcontroller Alloc]init];


Set C1 view color for this controller


C1.view.backgroundcolor=[uicolor Redcolor];


//


Uiviewcontroller *c2=[[uiviewcontroller Alloc]init];


C2.view.backgroundcolor=[uicolor Purplecolor];


//


Uiviewcontroller *c3=[[uiviewcontroller Alloc]init];


C3.view.backgroundcolor=[uicolor Browncolor];


//


Add these controllers to the navigation controller


[Nav pushviewcontroller:c1 Animated:yes];


[Nav pushviewcontroller:c2 Animated:yes];


[Nav pushviewcontroller:c3 Animated:yes];


}





Create three child control classes and corresponding Xib files


Copy Code code as follows:

YYONEVIEWCONTROLLER.M file
//
Yyoneviewcontroller.m
01-Use of the navigation controller 1
//
Created by Apple on 14-6-4.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYOneViewController.h"
#import "YYTwoViewController.h"

@interface Yyoneviewcontroller ()
/**
Jump to a second interface
*/
-(Ibaction) Jump2two: (ID) sender;

@end




Copy Code code as follows:

@implementation Yyoneviewcontroller


-(Ibaction) Jump2two: (ID) Sender {
1. Create a second child controller
Yytwoviewcontroller *two=[[yytwoviewcontroller Alloc]init];

2. The handle controller is added to the navigation controller
Is there any way to get the navigation controller?
As long as the current controller is a child controller of the navigation controller, it can be obtained directly from this property to the navigation controller where the current controller resides
[Self.navigationcontroller Pushviewcontroller:two Animated:yes];
}
@end




Copy Code code as follows:

YYTWOVIEWCONTROLLER.M file
//
Yytwoviewcontroller.m
01-Use of the navigation controller 1
//
Created by Apple on 14-6-4.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYTwoViewController.h"
#import "YYThreeViewController.h"
@interface Yytwoviewcontroller ()
-(Ibaction) Jump2three: (ID) sender;

@end




Copy Code code as follows:

@implementation Yytwoviewcontroller

Jump to a third child controller
-(Ibaction) Jump2three: (ID) Sender {
1. Create a third child controller
Yythreeviewcontroller *three=[[yythreeviewcontroller Alloc]init];
2. Add a child controller to the navigation controller
[Self.navigationcontroller Pushviewcontroller:three Animated:yes];

}
@end




Example: As long as the current controller is a child controller of the navigation controller, you can get directly to the navigation controller of the current controller through the Self.navigationcontroller property

Project file structure and operation effect:

Navigation controller properties and basic usage
First, some properties of navigation controller and basic use

1. Four ways to add the controller to the navigation controller

(1)

1. Create a navigation controller

Copy Code code as follows:

Uinavigationcontroller *nav=[[uinavigationcontrolleralloc]init];

2. Set the root view of the Navigation Controller as window

Copy Code code as follows:

Self.window.rootviewcontroller=nav;

3. Add

Copy Code code as follows:

Yyoneviewcontroller *one = [[Yyoneviewcontroller alloc] init];

[Nav Pushviewcontroller:one Animated:yes];

(2)

1. Create a navigation controller

Copy Code code as follows:

Uinavigationcontroller *nav=[[uinavigationcontrolleralloc]init];



2. Set the root view of the Navigation Controller as window


Copy Code code as follows:

Self.window.rootviewcontroller=nav;



3. Add


Copy Code code as follows:

Yyoneviewcontroller *one = [[Yyoneviewcontroller alloc] init];

[Nav Addchildviewcontroller:one];

(3)

1. Create a navigation controller

Copy Code code as follows:

Uinavigationcontroller *nav=[[uinavigationcontrolleralloc]init];



2. Set the root view of the Navigation Controller as window


Copy Code code as follows:

Self.window.rootviewcontroller=nav;



3. Add


Copy Code code as follows:

Yyoneviewcontroller *one = [[Yyoneviewcontroller alloc] init];

Nav.viewcontrollers=@[one];

(added to the stack of the navigation controller)

Description

Copy Code code as follows:
nav.viewcontrollers;== nav.childviewcontrollers;
Note that the property is read-only and therefore cannot be written as follows.


Copy Code code as follows:
Nav.childviewcontrollers = @[one];

(4) The most commonly used methods

Copy Code code as follows:

Yyoneviewcontroller *one=[[yyoneviewcontroller Alloc]init];

Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]initwithrootviewcontroller:one];




2. The title of the current sub-controller interface navigation bar and the settings for the corresponding return header

Copy Code code as follows:

self.navigationitem.title=@ "First Interface";

self.navigationitem.backbarbuttonitem=[[uibarbuttonitemalloc]initwithtitle:@ "return one" style: Uibarbuttonitemstyleplain Target:nilaction:nil];




3. Add a button to the navigation bar

Note: You can add one, or you can add multiple (arrays)

Add a button to the left of the navigation bar (a button that adds a camera icon) that will cover the back

Copy Code code as follows:

Self.navigationitem.leftbarbuttonitem=[[uibarbuttonitem Alloc]initwithbarbuttonsystemitem: Uibarbuttonsystemitemcamera Target:nil Action:nil];



4. Interface Jump

Jumps to the second interface (currently the third, removing the controller at the top of the stack)

Copy Code code as follows:

[Self.navigationControllerpopViewControllerAnimated:YES];




Remove all controllers except the stack bottom controller

Copy Code code as follows:

[Self.navigationControllerpopToRootViewControllerAnimated:YES];




As soon as one of the controllers in the stack is passed in, it jumps to the specified controller

Copy Code code as follows:

[Self.navigationcontroller poptoviewcontroller:<# (Uiviewcontroller *) #> animated:<# (BOOL) #>];




Second, the code example

YYAPPDELEGATE.M file

Copy Code code as follows:



//


Yyappdelegate.m


01-Use of the navigation controller 1


//


Created by Apple on 14-6-4.


Copyright (c) 2014 itcase. All rights reserved.


//

#import "YYAppDelegate.h"
#import "YYOneViewController.h"

@implementation Yyappdelegate

When the application is started, it is invoked


-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions


{


Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];


Self.window.backgroundColor = [Uicolor Whitecolor];








3. Add the child controller to the navigation controller


The first and most commonly used


Yyoneviewcontroller *one=[[yyoneviewcontroller Alloc]init];


Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]initwithrootviewcontroller:one];





1. Create a navigation controller


Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]init];


2. Set the root view of the Navigation Controller as window


Self.window.rootviewcontroller=nav;





Second Kind


Yyoneviewcontroller *one = [[Yyoneviewcontroller alloc] init];


[Nav Pushviewcontroller:one Animated:yes];





Third Kind


[Nav Addchildviewcontroller:one];


Fourth (added to the stack of the navigation controller)


Nav.viewcontrollers=@[one];





The stack of the navigation controller


nav.viewcontrollers;== nav.childviewcontrollers;


Note that this property is read-only and therefore cannot be written as follows


Nav.childviewcontrollers = @[one];








[Self.window makekeyandvisible];


return YES;


}

@end




YYONEVIEWCONTROLLER.M file


Copy Code code as follows:

//
Yyoneviewcontroller.m
01-Use of the navigation controller 1
//
Created by Apple on 14-6-4.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYOneViewController.h"
#import "YYTwoViewController.h"

@interface Yyoneviewcontroller ()
/**
Jump to a second interface
*/
-(Ibaction) Jump2two: (ID) sender;

@end




Copy Code code as follows:

@implementation Yyoneviewcontroller


-(Ibaction) Jump2two: (ID) Sender {
1. Create a second child controller
Yytwoviewcontroller *two=[[yytwoviewcontroller Alloc]init];

2. The handle controller is added to the navigation controller
Is there any way to get the navigation controller?
As long as the current controller is a child controller of the navigation controller, it can be obtained directly from this property to the navigation controller where the current controller resides
[Self.navigationcontroller Pushviewcontroller:two Animated:yes];
}

-(void) viewdidload
{
[Super Viewdidload];
Controls what the navigation bar for the current controller displays
self.navigationitem.title=@ "First Interface";
Modify what the return button displays
Self.navigationitem.backbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "return one" style: Uibarbuttonitemstyleplain Target:nil Action:nil];
}
@end




YYTWOVIEWCONTROLLER.M file


Copy Code code as follows:

//
Yytwoviewcontroller.m
01-Use of the navigation controller 1
//
Created by Apple on 14-6-4.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYTwoViewController.h"
#import "YYThreeViewController.h"
@interface Yytwoviewcontroller ()
-(Ibaction) Jump2three: (ID) sender;

@end




Copy Code code as follows:



@implementation Yytwoviewcontroller

Jump to a third child controller
-(Ibaction) Jump2three: (ID) Sender {
1. Create a third child controller
Yythreeviewcontroller *three=[[yythreeviewcontroller Alloc]init];
2. Add a child controller to the navigation controller
[Self.navigationcontroller Pushviewcontroller:three Animated:yes];
}

-(void) viewdidload


{


[Super Viewdidload];


Add a button to the navigation bar


Add a button to the left of the navigation bar (a button that adds a camera icon) that will cover the back


Self.navigationitem.leftbarbuttonitem=[[uibarbuttonitem Alloc]initwithbarbuttonsystemitem: Uibarbuttonsystemitemcamera Target:nil Action:nil];





Add multiple buttons to the right of the navigation bar


Create two buttons


Uibarbuttonitem *a=[[uibarbuttonitem Alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemstop target:nil Action : nil];


Uibarbuttonitem *b=[[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemreply Target:nil Action:nil];


Uibarbuttonitem *c=[[uibarbuttonitem Alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemcamera Target:nil Action:nil];


SELF.NAVIGATIONITEM.RIGHTBARBUTTONITEMS=@[A,B,C];





Sets the return of the corresponding navigation bar (the return of the third interface navigation bar)


Self.navigationitem.backbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "return" style: Uibarbuttonitemstylebordered Target:nil Action:nil];


}


@end





YYTHREEVIEWCONTROLLER.M file


Copy Code code as follows:

//
Yythreeviewcontroller.m
01-Use of the navigation controller 1
//
Created by Apple on 14-6-4.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYThreeViewController.h"
#import "YYTwoViewController.h"

@interface Yythreeviewcontroller ()
Return to the second controller page
-(Ibaction) Jump2two: (ID) sender;
Return to the first controller page
-(Ibaction) Jump2root: (ID) sender;

@end




Copy Code code as follows:

@implementation Yythreeviewcontroller


-(Ibaction) Jump2two: (ID) Sender {
Jumps to the second interface (removing the controller at the top of the stack)
[Self.navigationcontroller Popviewcontrolleranimated:yes];
}

-(Ibaction) Jump2root: (ID) Sender {
Remove all controllers except the stack bottom controller
[Self.navigationcontroller Poptorootviewcontrolleranimated:yes];

As soon as one of the controllers in the stack is passed in, it jumps to the specified controller
This cannot be done, not added to the navigation controller Yytwoviewcontroller *two = [[Yytwoviewcontroller alloc] init];
[Self.navigationcontroller poptoviewcontroller:<# (Uiviewcontroller *) #> animated:<# (BOOL) #>];
}
@end




Implementation effect:

Third, the navigation controller through the stack to manage the child controller

Diagram

Description

The navigation controller manages the child controller through the stack form (advanced and Out)

View that is displayed on the navigation controller and is always the top controller of the stack

A navigation controller has only one navigation bar, which means that all the controllers are common to one navigation bar.

Iv. Supplementary

In the proxy method, print all the child controls under the current window and save them through an XML file, as follows.

Copy Code code as follows:



The application gets the focus (which means that you can interact with the user)


-(void) Applicationdidbecomeactive: (uiapplication *) application


{


NSLog (@ "applicationdidbecomeactive");








Uinavigationcontroller *nav = (Uinavigationcontroller *) Self.window.rootViewController;


Uinavigationbar *bar = Nav.navigationbar;


NSLog (@ "%@", Nsstringfromcgrect (Bar.frame));





NSString *str = [self DigView:self.window];


[Str writetofile:@ "/users/apple/desktop/ios6.xml" atomically:yes];





}

/**


* Return all hierarchies of incoming veiw


*


* @param view needs to obtain a hierarchy of view


*


* @return String


*/


-(NSString *) Digview: (UIView *) view


{


if ([View Iskindofclass:[uitableviewcell class]]) return @ "";


1. Initialize


nsmutablestring *xml = [nsmutablestring string];





2. Label start


[XML appendformat:@ "&lt;%@ frame=\"%@\ "", View.class, Nsstringfromcgrect (View.frame)];
if (! Cgpointequaltopoint (View.bounds.origin, Cgpointzero)) {
[XML appendformat:@ "bounds=\"
%@\ "", Nsstringfromcgrect (View.bounds)];
}

if ([View Iskindofclass:[uiscrollview class]]) {
Uiscrollview *scroll = (Uiscrollview *) view;
if (! Uiedgeinsetsequaltoedgeinsets (Uiedgeinsetszero, Scroll.contentinset)) {
[XML appendformat:@ "contentinset=\"
%@\ "", Nsstringfromuiedgeinsets (Scroll.contentinset)];


}


}





3. Decide whether to end


if (View.subviews.count = = 0) {


[XML appendstring:@ "/&gt;"];


return XML;


} else {


[XML appendstring:@ "&gt;"];


}





4. Iterate through all the child controls


For (UIView *child in view.subviews) {


NSString *childxml = [self digview:child];


[XML Appendstring:childxml];


}





5. End of label


[XML appendformat:@ "&lt;/%@&gt;", View.class];





return XML;


}



Note: In iOS7 and previous versions, the different controls, including the child controller Interface frame.

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.