如何从一个故事板切换到另一个故事板

问题描述 投票:0回答:2

球队,

我有两个故事板。一个用于身份验证,另一个用于我的应用程序仪表板。

对于Authentication Storyboard初始化屏幕是loginScreen。登录成功后我正在加载Dashboard Storyboard。对于仪表板故事板,初始屏幕是MainViewController。

在这里,我在DashboardStoryboard中实现了注销。所以现在我想切换回我的身份验证故事板。

这里回到登录界面。但我认为这不是正确的实施方式。如果有一种方法可以做得更好会更有帮助吗?

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

非常感谢您的反馈。

ios objective-c iphone storyboard uistoryboard
2个回答
4
投票

使用segueStoryboard Reference非常容易。请按照步骤和屏幕截图进行操作

步骤1)

  • 从第一(主)故事板中的对象库中拖放Storyboard Reference

第2步)

  • 将来自ViewController的segue添加到Storyboard reference.

步骤-3)

  • 选择另一个(第二个)故事板。
  • 参考ID:您的destinationViewControler(第二个视图控制器)的StoryboardID,可在Second.Storyboard中找到


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

2
投票

这是一个诀窍,通过使用true设置密钥NsuserDefaults,当用户登录时,否则false并使用presentViewController方法相应地启动时导航您的应用程序没有动画,因此用户将无法获得任何选项以返回上一个vc。

看看下面代码说明上面的句子:

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];
}

如果你需要在vc出现时应用一些效果,只需在presentViewController方法之前看到这两行,请参阅:

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

注意:用户注销时将密钥设置为false。

© www.soinside.com 2019 - 2024. All rights reserved.