IOS Stroyboard Switch operation

Source: Internet
Author: User
Tags deprecated

There are three ways to switch between storyboard:

[Self presentviewcontroller:viewcontrollertopresent animated:yes completion:nil];

[Self.navigationcontroller pushviewcontroller:viewcontrollertopresent Animated:yes];

[Self.navigationcontroller setviewcontrollers:viewcontrollers Animated:yes];

The first two methods are on the basis of the current VC push operation,

The third method can arrange the current VC stack itself, a typical case is: the current login status, exit login, need to return to the login interface, this time the current login interface becomes the root interface (all the interface is not back), with Setviewcontrollers.

Program design for this:

1, New Empty Project

2, create the storyboard Firststoryboard, dragged into the navigation controller,tab Bar Controller (automatically generated 2 ordinary view controller), and then dragged into a view controller, Becomes the Third tab of the tab Bar Controller.

The Navigation controller and TAB Bar controller use the root view to segue between the Segue,tab Bar controller and the view controller using the view controllers.

Change the storyboard ID in the Identity Properties panel of the tab Bar controller to Tabbar.

Changing the name of the tab bar item for the view controller is TAB1,TAB2,TAB3, and then the display name of the tab bar controller becomes tab1,tab2,tab3.

Add a button to the second view controller that corresponds to TAB2, and the button text is: This is Firststoryboard, click Switch to Secondstoryboard.

The wizard adds the ViewControllerFirstTab2 class, inherits the Uiviewcontroller, increments the action action of the button, and adds the code later.

3, create the storyboard secondstoryboard, drag into the navigation Controller,view Controller, segue with Root View.

Change the storyboard ID in the Identity Properties panel of the view controller to Senondviewcontroller.

Add the first button to the View controller, the button text is: This is Secondstoryboard, click Switch to Secondstoryboard.

To add a second button to the View controller, the button text is: This is Secondstoryboard, click Switch to Firststoryboard.

The wizard adds the Viewcontrollersecond class, adds the action action for the button, and adds the code later.

4, create the storyboard thirdstoryboard, drag into the navigation Controller,view Controller, segue with Root View.

Change the view controller's show the Identity Inspector property panel with the storyboard ID of Threeviewcontroller.

Add the first button to the View controller, the button text is: This is Secondstoryboard, click Switch to Secondstoryboard.

To add a second button to the View controller, the button text is: This is Secondstoryboard, click Switch to Firststoryboard.

The wizard adds the Viewcontrollerthird class, adds the action action for the button, and adds the code later.

Let's start adding code:

1,

APPDELEGATE.M Code:

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

{

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

Override point for customization after application launch.

Self.window.backgroundColor = [Uicolor Whitecolor];

[Self.window makekeyandvisible];

Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Firststoryboard" bundle:nil];

Self.window.rootviewcontroller=[storyboard Instantiateinitialviewcontroller];

return YES;

}

2,

ViewControllerFirstTab2.h Code:

@interface Zviewcontrollerfirsttab2:uiviewcontroller

-(Ibaction) Buttonactionsecond: (ID) sender;

@end

viewcontrollerfirsttab2.m Code:

-(Ibaction) Buttonactionsecond: (ID) Sender {

NSString *nsstringstoryboard = @ "Secondstoryboard";

NSString *NSSTRINGVC = @ "Second";

Uistoryboard *storyboard = [Uistoryboard storyboardwithname:nsstringstoryboard bundle:nil];

if (nil = = Storyboard) NSLog (@ "NO%@", Nsstringstoryboard);

Uiviewcontroller *VC = [Storyboard INSTANTIATEVIEWCONTROLLERWITHIDENTIFIER:NSSTRINGVC];

if (nil = = VC) NSLog (@ "NO%@", NSSTRINGVC);

/*

The method directly operates the VC, does not need nav, and the function corresponding to Presentviewcontroller is dismissviewcontrolleranimated

The function has been deprecated

[Self Presentmodalviewcontroller:[storyboard Instantiateinitialviewcontroller] animated: (TRUE)];

[Self PRESENTVIEWCONTROLLER:VC animated:yes completion:nil];

*/

if (nil = = Self.navigationcontroller) NSLog (@ "null Navigationcontroller");

Else

{

Self.navigationController.navigationBarHidden = YES;

[Self.navigationcontroller PUSHVIEWCONTROLLER:VC Animated:yes];

}

}

3,

ViewControllerSecond.h Code:

@interface Zviewcontrollersecond:uiviewcontroller

-(Ibaction) Buttonactionthree: (ID) sender;

-(Ibaction) Buttonactionone: (ID) sender;

@end

viewcontrollersecond.m Code:

-(Ibaction) Buttonactionthree: (ID) Sender {

NSString *nsstringstoryboard = @ "Thirdstoryboard";

NSString *NSSTRINGVC = @ "Third";

Uistoryboard *storyboard = [Uistoryboard storyboardwithname:nsstringstoryboard bundle:nil];

if (nil = = Storyboard) NSLog (@ "NO%@", Nsstringstoryboard);

Uiviewcontroller *VC = [Storyboard INSTANTIATEVIEWCONTROLLERWITHIDENTIFIER:NSSTRINGVC];

if (nil = = VC) NSLog (@ "NO%@", NSSTRINGVC);

/*

The method directly operates the VC, does not need nav, and the function corresponding to Presentviewcontroller is dismissviewcontrolleranimated

The function has been deprecated

[Self Presentmodalviewcontroller:[storyboard Instantiateinitialviewcontroller] animated: (TRUE)];

[Self PRESENTVIEWCONTROLLER:VC animated:yes completion:nil];

*/

if (nil = = Self.navigationcontroller) NSLog (@ "null Navigationcontroller");

Else

{

Self.navigationController.navigationBarHidden = YES;

[Self.navigationcontroller PUSHVIEWCONTROLLER:VC Animated:yes];

}

}

-(Ibaction) Buttonactionone: (ID) Sender {

When the first interface is returned, the first interface retains the state of the switch

[Self dismissviewcontrolleranimated:yes completion:nil];

[Self.navigationcontroller Popviewcontrolleranimated:yes];

}

4,

ViewControllerThird.h Code:

@interface Zviewcontrollerthird:uiviewcontroller

-(Ibaction) Buttonactionsecond: (ID) sender;

-(Ibaction) Buttonactionfirst: (ID) sender;

@end

viewcontrollerthird.m Code:

-(Ibaction) Buttonactionsecond: (ID) sender

{

[Self dismissviewcontrolleranimated:yes completion:nil];

[Self.navigationcontroller Popviewcontrolleranimated:yes];

}

-(Ibaction) Buttonactionfirst: (ID) sender

{

NSString *nsstringstoryboard = @ "Firststoryboard";

NSString *NSSTRINGVC = @ "TABBAR";

Uistoryboard *storyboard = [Uistoryboard storyboardwithname:nsstringstoryboard bundle:nil];

Uiviewcontroller *VC = [Storyboard INSTANTIATEVIEWCONTROLLERWITHIDENTIFIER:NSSTRINGVC];

if (nil = = Self.navigationcontroller) NSLog (@ "null Navigationcontroller");

Nsmutablearray *viewcontrollers = [Self.navigationController.viewControllers mutablecopy];

[Viewcontrollers removeallobjects];

[Viewcontrollers ADDOBJECT:VC];

NSLog (@ "%d", [viewcontrollers Count]);

[Self.navigationcontroller setviewcontrollers:viewcontrollers Animated:yes];

}

IOS Stroyboard Switch operation

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.