Programmatically set the initial view controller using Storyboards

前端 未结 22 1921
感情败类
感情败类 2020-11-22 15:00

How do I programmatically set the InitialViewController for a Storyboard? I want to open my storyboard to a different view depending on some condition which may

相关标签:
22条回答
  • 2020-11-22 15:20

    UPDATED ANSWER for iOS 13 and scene delegate:

    make sure in your info.plist file you go into Application Scene Manifest -> Scene Configuration -> Application Session Role -> Item 0 and delete the reference to the main storyboard there as well. Otherwise you'll get the same warning about failing to instantiate from storyboard.

    Also, move the code from the app delegate to the scene delegate method scene(_:willConnectTo:options:), since this is where life cycle events are handled now.

    0 讨论(0)
  • 2020-11-22 15:20

    Select the view controller which you want to open first and go to attribute inspector. Go to initial scene and check is initial view controller option.

    Now this will be your initial view controller that will open first when application launch.

    0 讨论(0)
  • 2020-11-22 15:21

    Open mainstoryboard, select the view that you want start first, then open Utilities--> Attributes. Below the "View Controller" you see the "Is initial View Controller" radio button. Just select it.

    --- To the revised question:

    May be you can try this: write a method in ViewDidLoad section of your inital view and when the method runs on application launch, method triggers a segue to another view.

    0 讨论(0)
  • 2020-11-22 15:21

    Another solution with using Swift 3 and Swift 4 with avoiding force casting is like this

    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
    }
    

    And below is using with 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
    }
    
    0 讨论(0)
  • 2020-11-22 15:23

    In AppDelegate.swift you can add the following code:

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

    Of course, you need to implement your logic, based on which criteria you'll choose an appropriate view controller.

    Also, don't forget to add an identity (select storyboard -> Controller Scene -> Show the identity inspector -> assign StorboardID).

    0 讨论(0)
  • 2020-11-22 15:23
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            self.window = UIWindow(frame: UIScreen.main.bounds)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            if (PreferenceHelper.getAccessToken() != "") {
                let initialViewController = storyboard.instantiateViewController(withIdentifier: "your View Controller Identifier")
                self.window?.rootViewController = initialViewController
            } else {
                let initialViewController = storyboard.instantiateViewController(withIdentifier: "your View Controller identifier")
                self.window?.rootViewController = initialViewController
            }
            self.window?.makeKeyAndVisible()
            return true
        }
    
    /*
    use your view Controller identifier must use it doubles quotes**strong text**
    
    0 讨论(0)
提交回复
热议问题