在 AppDelegate 中以编程方式设置 App 入口点

Set App Entry point programmatically in AppDelegate

在我的 appdelegate 中,我想检查是否 globUs.hasName。如果是这样,我希望应用程序的 Entry Point 成为我的 main 故事板。如果不是,我希望应用程序的 Entry Point 成为我的 newUser 故事板。如何设置应用程序的入口点?如果我不能,实现此功能的最有效方法是什么?

尝试

var sb = UIStoryboard(name: "OneStoryboard", bundle: nil)
/// Load initial view controller
var vc = sb.instantiateInitialViewController()
/// Or load with identifier
var vc = instantiateViewController(withIdentifier: "foobarViewController")

/// Set root window and make key and visible
self.window = UIWindow(frame: UIScreen.mainScreen.bounds)
self.window.rootViewController = vc
self.window.makeKeyAndVisible()

或尝试在情节提要中手动转场。要执行手动转场,您必须首先在故事板中定义一个带有标识符的转场,然后在视图控制器中调用 performSegue(withIdentifier:sender:)

考虑不设置入口点。然后,在 appDelegate 中,测试您的变量并相应地选择合适的故事板。然后显示该故事板中的视图控制器。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if globUs.hasName {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "FirstMainVC")
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = new
        self.window?.makeKeyAndVisible()
    }
    else {
        let storyboard = UIStoryboard(name: "NewUser", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "FirstNewUserVC")
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = welcomeVC
        self.window?.makeKeyAndVisible()
    }

    return true
}