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

时间:2017-02-11 01:56:55

标签: ios swift startup appdelegate entry-point

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

2 个答案:

答案 0 :(得分:4)

考虑没有入口点。然后,在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
}

答案 1 :(得分:0)

尝试

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()

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