使用故事板编程设置初始视图控制器

267

我如何以编程的方式设置Storyboard中的InitialViewController?我想根据某些条件,在每次启动时打开Storyboard的不同视图。


1
请查看此答案,无需在设置中清除主故事板或警告。链接 - Borzh
25个回答

1

Swift 5或以上版本#使用这个简单的代码来创建路由视图控制器。 如果您使用的是xcode 11或以上版本,请在AppDelegate中首先初始化var window: UIWindow?

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

0

使用故事板设置初始视图控制器(而不是主要视图控制器)

[Swift 5和Xcode 11]

  1. Main.storyboard -> 视图控制器 -> 属性检查器 -> 取消选中 Is Initial View Controller

  2. 应用程序目标 -> 通用 -> 从 Main Interface 中删除所有内容

  3. 编辑应用程序委托

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: UIScreen.main.bounds)
        
        let storyboard: UIStoryboard = UIStoryboard(name: "SomeStoryboard", bundle: nil) //SomeStoryboard  is name of .storyboard
        let viewController: ViewController = storyboard.instantiateViewController(withIdentifier: "someStoryboardId") as! ViewController //someStoryboardId is Storyboard ID

        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()

        return true
    }
}

0

以下是Objective C代码。

以下代码将创建您的自定义入口文件而不使用故事板。

我在Stack Overflow上阅读的所有答案都具有以下代码,但它们缺少重要行。

MainViewController是我希望成为首个要加载的视图控制器。

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    UIWindowScene *winScreen = (UIWindowScene *)scene;
    self.window = [[UIWindow alloc] initWithWindowScene:winScreen];
    
    // Create a root view controller and set it as the root view controller of the navigation controller
    MainViewController *rootViewController = [[MainViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    
    // Set the navigation controller as the root view controller of the window
    [self.window setRootViewController:navigationController];
    [self.window makeKeyAndVisible];
}

在这里我不知道为什么,但每个答案都没有提到第一行

UIWindowScene *winScreen = (UIWindowScene *)scene;

这段代码应该添加到SceneDelegate.m文件中。 现在我们可以删除main.storyboard文件,删除此文件后,我们还必须从info.plist中删除两个引用。

in project configuration > Info tab > delete Main screen interface file base name
also in project configuration > Info tab > Application scene manifest > Scene configuration > Application session role > Item 0 > delete Storyboard name

图片,指示要删除哪个参考

注意:此方法在我发布这篇答案时有效,但随着新版本的发布,可能会失效,因此请保持更新。


这并没有真正回答问题。如果您有其他问题,可以单击提问来提出。如果您想在此问题有新的答案时得到通知,您可以关注此问题。一旦您获得足够的声望,您也可以添加悬赏以吸引更多注意力。- 来自审核 - user20384561

0

Xcode 12.4 Swift 5

SceneDelegate.Swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    let window = UIWindow(windowScene: windowScene)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    window.rootViewController = storyboard.instantiateViewController(withIdentifier: "UserViewController") as! UserViewController
    
    self.window = window
    window.makeKeyAndVisible()
}

您可以添加条件,以确定哪个视图控制器场景将显示


-3

选择您想要首先打开的视图控制器,然后转到属性检查器。

进入初始场景并检查“是初始视图控制器”选项。

现在这将是您的初始视图控制器,在应用程序启动时首先打开。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接