Swift 3, Xcode 8 Instantiate View Controller is not working
Asked Answered
C

4

16

Xcode 8 when it compiles says to change instantiate viewcontroller with identifier to simply, instantiate view controller. I did that, why does it give two errors?

I'm working with Swift 3. I want to change pages programmatically.I've read a lot of other questions on the topic. All of them use instantiate view controller with the identifier. They haven't adopted the new language.

@IBAction func switchPage(_ sender: UIButton) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = 
storyboard.instantiateViewController("secondViewController") as! 
UIViewController
    self.presentViewController(secondViewController, animated: true, 
completion: nil)    

}

Thanks. I changed the code as suggested, and receive only one error: Value of optional type 'UIStoryboard?' not unwrapped; did you mean to use '!' or '?'? Should I add an exclamation point somewhere?

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a 
nib.
}

@IBAction func ch(_ sender: UIButton) {



    let viewController = 
storyboard.instantiateViewController(withIdentifier: 
"secondViewController") as! UIViewController
    self.present(viewController, animated: true)



}




override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}
Chrysoprase answered 29/9, 2016 at 17:52 Comment(0)
M
44

Try like this.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"secondViewController") as! UIViewController
self.present(viewController, animated: true)    
Mailable answered 29/9, 2016 at 17:56 Comment(8)
Thanks. It's close, but I'm getting one error. Any suggestions?Chrysoprase
@Chrysoprase I have never told you to remove this line let storyboard = UIStoryboard(name: "Main", bundle: nil) check the edited answer.Mailable
Welcome mate :)Mailable
@NiravD: will this crash the app if there isn't a viewController with that ID in the storyboard?Benighted
@3000 Yes that obviousMailable
@NiravD: and should your code consider this possibility?Benighted
@3000 The problem in OP code is it forgot to add first parameter label withIdentifier it has nothing to do with the error that you are facing if you are not having identifier in your storyboard then it will crash the appMailable
@NiravD: my comment was generic and not related to the OP issue: moreover, I've always written the same code as yours, without caring too much :-)Benighted
Q
5

It's worked for me:

 let gameScene = UIStoryboard(name: "Main", bundle:nil).instantiateViewController(withIdentifier: "ViewController") as UIViewController
        let appDelegate = (UIApplication.shared.delegate as! AppDelegate)
        appDelegate.window?.rootViewController = gameScene
Quintic answered 17/10, 2016 at 1:3 Comment(1)
This is the best granteed way 👍🏻Enzymolysis
B
3

It's worked for me by this:

let vc = UIStoryboard(name: "ZWLStaticTabVc", bundle: nil).instantiateInitialViewController()
self.navigationController?.pushViewController(vc!, animated: true)
Brat answered 14/10, 2016 at 2:56 Comment(0)
S
1

In my case I had forgotten to check isInitialViewController for my VC in storyboard. Hence it was returning nil when I called instantiateInitialViewController. After checking that option in storyboard, the issue resolved.

Sempstress answered 4/11, 2020 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.