Programmatically set the initial view controller using Storyboards

前端 未结 22 1939
感情败类
感情败类 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:05
    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
    

    Another way is to present viewController,

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

    First you need to create object of your storyboard then change root(if required) then you take reference of particular view controller which is pushed current view controller(if you change root) else it's just present new view controller which may you

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

    I don't think it's possible. Instead you can have one initial controller which will have segues to different view controllers. On startup, you can decide which segue to perform programmatically.

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

    Thanks modified this as follows in AppDelegate:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) ->     Bool {
    //Some code to check value of pins
    
    if pins! == "Verified"{
            print(pins)
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            let mainStoryboard: UIStoryboard = UIStoryboard(name: "HomePage", bundle: nil)
            let exampleViewController: UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("SBHP") as! UINavigationController
    
            self.window?.rootViewController = exampleViewController
    
            self.window?.makeKeyAndVisible()
        }else{
            print(pins)
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let exampleViewController: UINavigationController = mainStoryboard.instantiateViewControllerWithIdentifier("SBUser") as! UINavigationController
    
            self.window?.rootViewController = exampleViewController
    
            self.window?.makeKeyAndVisible()
        }
    
    0 讨论(0)
  • 2020-11-22 15:07

    For all the Swift lovers out there, here is the answer by @Travis translated into SWIFT:

    Do what @Travis explained before the Objective C code. Then,

    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
    }
    

    The ExampleViewController would be the new initial view controller you would like to show.

    The steps explained:

    1. Create a new window with the size of the current window and set it as our main window
    2. Instantiate a storyboard that we can use to create our new initial view controller
    3. Instantiate our new initial view controller based on it's Storyboard ID
    4. Set our new window's root view controller as our the new controller we just initiated
    5. Make our new window visible

    Enjoy and happy programming!

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

    A few days ago I've encountered to same situation. A very simple trick solved this problem. I set hidden my initial view controller before launch2. If initial view controller is the right controller it's set to visible in viewDidLoad. Else, a segue is performed to desired view controller. It works perfectly in iOS 6.1 and above. I'm sure it works on earlier versions of iOS.

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

    Found simple solution - no need to remove "initial view controller check" from storyboard and editing project Info tab and use makeKeyAndVisible, just place

    self.window.rootViewController = rootVC;
    

    in

    - (BOOL) application:didFinishLaunchingWithOptions:
    
    0 讨论(0)
提交回复
热议问题