OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Storyboard 中加载 ViewController [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:36
标题: ios - 在 Storyboard 中加载 ViewController

我正在使用侧 View Controller 设置:https://github.com/edgecase/ECSlidingViewController

使用标识符为“InitialViewController”的 Storyboard加载初始 View Controller 。

一旦为此加载到 viewDidLoad 中,我会使用以下内容检查用户是否已登录:

UIStoryboard *storyboard;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    storyboard = [UIStoryboard storyboardWithName"MainStoryboard_iPhone" bundle:nil];
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyboard = [UIStoryboard storyboardWithName"MainStoryboard_iPad" bundle:nil];
}

if (![PFUser currentUser]){
    self.topViewController = [storyboard instantiateViewControllerWithIdentifier"WelcomeVC"];
} else {
    self.topViewController = [storyboard instantiateViewControllerWithIdentifier"HomeVC"];
}

根据上述,如果用户没有登录,它会加载 WelcomeVC。 WelcomeVC 是一个导航 Controller ,其中包含 3 个 Vc。欢迎/登录/注册。

用户登录后,我需要将 topViewController(如上)更改为 HomeVC。如果可能,可以关闭名为 WelcomeVC 的导航 Controller 。 如何更改此 topViewController 我已尝试过,但它不起作用:

UIStoryboard *storyboard;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    storyboard = [UIStoryboard storyboardWithName"MainStoryboard_iPhone" bundle:nil];
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyboard = [UIStoryboard storyboardWithName"MainStoryboard_iPad" bundle:nil];
}

self.EVC = [storyboard instantiateViewControllerWithIdentifier"InitialViewController"];
self.EVC.topViewController = [storyboard instantiateViewControllerWithIdentifier"HomeVC"];



Best Answer-推荐答案


就个人而言,我喜欢从一开始就将主屏幕设置为 Root View Controller ,并让 viewDidAppear 通过非动画模式 segue 自动将我带到登录屏幕(我更喜欢 performSegueWithIdentifier 以便 Storyboard 的 segues 显示整个流程,而不是手动实例化 Controller 并调用 pushViewController,但不管对你有用)。然后,我在成功登录后,只需返回主屏幕即可。这样一来,我就不必更改顶级 Controller 了。

所以我的 Storyboard看起来像:

enter image description here

所以我的主“家” View Controller 具有以下 viewDidAppear:

- (void)viewDidAppearBOOL)animated
{
    [super viewDidAppear:animated];

    if (![[Model sharedManager] loggedIn])
        [self performSegueWithIdentifier"login" sender:self];
}

显然,您确定是否登录的逻辑会有所不同,但您明白了。我的登录对登录屏幕进行了模态转换(无动画),而我恰好在一系列登录屏幕上使用了导航 Controller 。

然后我的最终成功登录过程是:

- (IBAction)didTouchUpInsideDoneButtonid)sender
{
    // ok, assuming at this point that everything has been validated and 
    // I'm ready to return, so just dismiss the navigation controller.
    // You could use unwind segue in iOS 6, as well.

    [[Model sharedManager] setLoggedIn:YES];

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

关于ios - 在 Storyboard 中加载 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16025053/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4