我如何以编程的方式设置Storyboard中的InitialViewController
?我想根据某些条件,在每次启动时打开Storyboard的不同视图。
我如何以编程的方式设置Storyboard中的InitialViewController
?我想根据某些条件,在每次启动时打开Storyboard的不同视图。
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()
使用故事板设置初始视图控制器(而不是主要视图控制器)
Main.storyboard
-> 视图控制器 -> 属性检查器 -> 取消选中 Is Initial View Controller
应用程序目标 -> 通用 -> 从 Main Interface
中删除所有内容
编辑应用程序委托
@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
}
}
以下是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
注意:此方法在我发布这篇答案时有效,但随着新版本的发布,可能会失效,因此请保持更新。
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()
}
您可以添加条件,以确定哪个视图控制器场景将显示
选择您想要首先打开的视图控制器,然后转到属性检查器。
进入初始场景并检查“是初始视图控制器”选项。
现在这将是您的初始视图控制器,在应用程序启动时首先打开。