以编程方式在AppDelegate中设置应用程序入口点

问题描述:

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

+0

只需将self.window?.rootViewController设置为您在didFinishLaunchingWithOptions函数中的首选viewController。 –

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

当我使用类似于这个AppDel的方法抛出一个SIGABRT。它说“'在包NSBundle Users/gabrielspound/Library/Developer/CoreSimulator/Devices/6CB76037-BA27-45DA-8671-01AAF78E93A6/data/Containers/Bundle/Application/0B5AC724>中找不到名为'OneStoryboard'的故事板-BEF5-4DB6-A68F-8407A6ECF627/Social Justice.app>(loaded)'“ UIStoryboard构造函数中的”name:“字段的含义是什么?它是任意的还是必须使用我的Main.storyboard的文件名? –

+0

这工作,谢谢! –

+0

@GabeSpound是的,你正在使用文件名 – Shades

尝试

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。要执行手动搜索,您必须首先在故事板中定义带有标识符的搜索队列,然后在视图控制器中调用performSegue(withIdentifier:sender:)

+0

查看评论我留在上面的答案,它也适用于这里。 –

+0

我得到了这个工作,但其他方法似乎更有效 –