我如何以编程方式设置一个故事板的InitialViewController ?我想打开我的故事板到一个不同的视图这取决于不同的启动条件。


当前回答

斯威夫特5

如果你没有一个ViewController设置为storyboard中的初始ViewController,你需要做两件事:

进入你的项目TARGETS,选择你的项目-> General ->清除Main Interface字段。 总是在项目TARGETS中,现在转到Info ->应用场景清单->场景配置->应用程序会话角色-> Item0(默认配置)->删除故事板名称字段。

最后,你现在可以在SceneDelegate中添加你的代码:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }

    window = UIWindow(windowScene: windowScene)


    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    // Make sure you set an Storyboard ID for the view controller you want to instantiate
    window?.rootViewController = storyboard.instantiateViewController(withIdentifier: identifier)
    window?.makeKeyAndVisible()

}

其他回答

使用Swift 3和Swift 4避免强制铸造的另一个解决方案是这样的

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    guard let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewController") as? YourViewController else {
        return false
    }
    self.window?.rootViewController = viewController
    self.window?.makeKeyAndVisible()
    return true
}

下面是使用UINavigationController

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    guard let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewController") as? YourViewController else {
        return false
    }
    let navigationController = UINavigationController(rootViewController: viewController)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
    return true
}

选择要首先打开的视图控制器,然后转到属性检查器。 转到初始场景,检查初始视图控制器选项。

这是你的初始视图控制器在应用启动时首先打开。

Swift 5或以上版本#创建路由视图控制器 如果你使用xcode 11或以上版本,首先初始化var window: UIWindow?在AppDelegate

let rootVC = mainStoryboard.instantiateViewController(withIdentifier: "YOURCONTROLLER") as! YOURCONTROLLER

        navigationController.setNavigationBarHidden(true, animated: true)
        UIApplication.shared.windows.first?.rootViewController = UINavigationController.init(rootViewController: rootVC)
        UIApplication.shared.windows.first?.makeKeyAndVisible()

(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions你可以通过编程方式设置(BOOL)应用程序中的关键窗口的rootViewController

例如:

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (shouldShowAnotherViewControllerAsRoot) {
        UIStoryboard *storyboard = self.window.rootViewController.storyboard;
        UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"rootNavigationController"];
        self.window.rootViewController = rootViewController;
        [self.window makeKeyAndVisible];
    }

    return YES;
}

找到了简单的解决方案-不需要从故事板和编辑项目信息选项卡中删除“初始视图控制器检查”,并使用makeKeyAndVisible,只是放置

self.window.rootViewController = rootVC;

in

- (BOOL) application:didFinishLaunchingWithOptions: