Instantiate and Present a viewController in Swift

前端 未结 16 1539
难免孤独
难免孤独 2020-11-22 13:04

Issue

I started taking a look of the new Swift on Xcode 6, and I tried some demo projects and tutorials. Now I am stuck at:

相关标签:
16条回答
  • 2020-11-22 13:11

    Swift 5

    let vc = self.storyboard!.instantiateViewController(withIdentifier: "CVIdentifier")
    self.present(vc, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-11-22 13:12

    For people using @akashivskyy's answer to instantiate UIViewController and are having the exception:

    fatal error: use of unimplemented initializer 'init(coder:)' for class

    Quick tip:

    Manually implement required init?(coder aDecoder: NSCoder) at your destination UIViewController that you are trying to instantiate

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    

    If you need more description please refer to my answer here

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

    This link has both the implementations:

    Swift:

    let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
    self.presentViewController(viewController, animated: false, completion: nil)
    

    Objective C

    UIViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
    

    This link has code for initiating viewcontroller in the same storyboard

    /*
     Helper to Switch the View based on StoryBoard
     @param StoryBoard ID  as String
    */
    func switchToViewController(identifier: String) {
        let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(identifier) as! UIViewController
        self.navigationController?.setViewControllers([viewController], animated: false)
    
    }
    
    0 讨论(0)
  • 2020-11-22 13:12

    Swift 4.2 updated code is

    let storyboard = UIStoryboard(name: "StoryboardNameHere", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerNameHere")
    self.present(controller, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-11-22 13:14

    I know it's an old thread, but I think the current solution (using hardcoded string identifier for given view controller) is very prone to errors.

    I've created a build time script (which you can access here), which will create a compiler safe way for accessing and instantiating view controllers from all storyboard within the given project.

    For example, view controller named vc1 in Main.storyboard will be instantiated like so:

    let vc: UIViewController = R.storyboard.Main.vc1^  // where the '^' character initialize the controller
    
    0 讨论(0)
  • 2020-11-22 13:15

    This answer was last revised for Swift 5.2 and iOS 13.4 SDK.


    It's all a matter of new syntax and slightly revised APIs. The underlying functionality of UIKit hasn't changed. This is true for a vast majority of iOS SDK frameworks.

    let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "myVCID")
    self.present(vc, animated: true)
    

    If you're having problems with init(coder:), please refer to EridB's answer.

    0 讨论(0)
提交回复
热议问题