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


当前回答

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "storyBoardid") as! ViewController
let navigationController = UINavigationController(rootViewController: vc)
UIApplication.shared.delegate.window?.rootViewController = navigationController

另一种方法是present viewController,

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "storyBoardid") as! ViewController
self.present(vc,animated:true,completion:nil)

首先你需要创建storyboard的对象然后改变根(如果需要)然后你引用特定的视图控制器它会被推送当前视图控制器(如果你改变根)否则它会呈现新的视图控制器

其他回答

对于所有喜欢霉霉的人,下面是@Travis用Swift翻译的答案:

做@Travis在Objective C代码之前解释的事情。然后,

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var exampleViewController: ExampleViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ExampleController") as! ExampleViewController

    self.window?.rootViewController = exampleViewController

    self.window?.makeKeyAndVisible()

    return true
}

ExampleViewController将是您想要显示的新的初始视图控制器。

步骤解释如下:

创建一个与当前窗口大小相同的新窗口,并将其设置为主窗口 实例化一个故事板,我们可以用它来创建新的初始视图控制器 基于Storyboard ID实例化我们新的初始视图控制器 将新窗口的根视图控制器设置为我们刚刚启动的新控制器 使新窗口可见

享受和快乐的编程!

如果你不想改变applicationDidFinish,你可以做下面的技巧:

设置导航控制器为初始视图控制器,并分配给它一个自定义类'MyNavigationController'。然后你可以在viewDidLoad期间调整它的根视图控制器——它会覆盖你在故事板中设置的根视图控制器。

class MyNavigationController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLoggedIn() {
            viewControllers = [R.storyboard.authentication.loginView()!]
        }
    }

    private func isLoggedIn() -> Bool {
        return false
    }

}

在AppDelegate.swift中,你可以添加以下代码:

let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "YourViewController_StorboardID")
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()

当然,你需要实现你的逻辑,基于什么标准你将选择一个合适的视图控制器。

另外,不要忘记添加一个标识(select storyboard -> Controller Scene -> Show the identity inspector -> assign StorboardID)。

(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;
}

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

你可以添加视图控制器场景将要显示的条件