简体   繁体   中英

How to switch from one storyboard to another storyboard

Team,

I have two storyboard. One For authentication And another for My application Dashboard.

For Authentication Storyboard The initialisation screen is loginScreen. After login successfully i am loading Dashboard Storyboard. For dashboard storyboard the initial screen is MainViewController.

Here I am implemented logout from in DashboardStoryboard. So now i want to switch back to my Authentication Storyboard.

Here its going back to loginScreen. But I thing its not proper way for implementing. It will be more helpful is there a way I can do better?

-(void)logout{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Authentication" bundle: nil];
    LoginViewScreenController *loginViewScreenController = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewScreenController"];
    [self.navigationController pushViewController: loginViewScreenController animated:NO];
}

Your feedback is highly appreciated.

It's Very easy using segue and Storyboard Reference . Please follwo steps and screenshots.

Step-1)

  • Drag and drop Storyboard Reference from Object Library in First(Main) Story board.

Step-2)

  • Add segue from your source ViewController to Storyboard reference.

Step-3)

  • Select another(second) storyboard.

  • Reference ID: StoryboardID of your destinationViewControler(second View Controller) which is available in Second.Storyboard


-(void)logout
{
    UIViewController *aVCObj = [[UIApplication sharedApplication]delegate].window.rootViewController;
    if ([aVCObj isKindOfClass:[UINavigationController class]]) {
        UINavigationController *aNavController = (UINavigationController *)aVCObj;
        [aNavController popToRootViewControllerAnimated:YES];
    }
}

Here is a trick to do this by setting up a key true using NsuserDefaults when user logged in otherwise false and navigate your application when starts accordingly using presentViewController method without animation so the user will not get any option to go back previous vc.

Have a look below code illustrating above sentence:

if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"isloggedIn"] isEqualToString:@"true"]) {
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"loginView"];
    [self presentViewController:vc animated:NO completion:nil];

}else{        // when logout
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"logoutView"];
    [self presentViewController:vc animated:NO completion:nil];
}

If you need to apply some effects when vc appear just ad these two lines before presentViewController method see:

[vc setModalPresentationStyle:UIModalPresentationCustom];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

Note: set the key false when user logout.

    let sb = UIStoryboard(name: "Main", bundle: nil)
    let homeVC = sb.instantiateViewController(withIdentifier: "TabBarController")
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = homeVC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM