可以将performSegueWithIdentifier与AppDelegate一起使用吗?

rad*_*ven 5 uisplitviewcontroller ios uistoryboard segue

我正在开发一个应用程序,在启动时检查有效的登录凭据,如果找到并且未过期,则会显示主拆分视图控制器,如果不是,则应显示登录屏幕.

每个部分都可以单独工作,但我正在努力在发布时选择合适的视图进行显示.

我已经尝试从根视图控制器设置模态segue,并在我的应用程序中:在App Delegate中使用didFinishLaunchingWithOptions:函数,调用:

// Segue to the login view controller...
if (loginNeeded) {
    [self.window.rootViewController performSegueWithIdentifier:@"LoginScreen" sender:self];
}
Run Code Online (Sandbox Code Playgroud)

这在逻辑上应该可行,但是从app委托中触发segue似乎是不可能的.

处理这个问题的理想场所和技术是什么?

And*_*dom 4

您可以尝试自定义转场,按照这篇文章hide-a-segue-on-login-process

或者,如果您迫切希望在分割视图控制器加载之前显示登录信息,请尝试以下操作......

在主故事板上创建登录屏幕,例如,作为UIViewController. 确保它是初始场景(检查Is Initial View Controller)。

在故事板上,创建一个从登录类到原始 SplitViewController 的新 Segue。给它一个标识符“ Load SplitViewController”和一个我们称之为 的 segue 自定义类名FullyReplaceSegue

在登录类 .m 文件中,添加用户登录后要调用的代码:

[self performSegueWithIdentifier:@"Load SplitViewController" sender:self];
Run Code Online (Sandbox Code Playgroud)

根据上面的内容创建新的 segue 类UIStoryboardSegue并为其命名FullyReplaceSegue

.h 文件

#import <UIKit/UIKit.h>
@interface  : UIStoryboardSegue

@end
Run Code Online (Sandbox Code Playgroud)

.m 文件

#import "FullyReplaceSegue.h"

@implementation FullyReplaceSegue

- (void)perform
{
    UIViewController *dest = (UIViewController *) super.destinationViewController;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = dest;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        UISplitViewController *splitViewController = (UISplitViewController *)dest;  // assumes we're transitioning to a UISplitViewController!
        UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
        splitViewController.delegate = (id)navigationController.topViewController;
    }
}

@end
Run Code Online (Sandbox Code Playgroud)