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.
}



}
Aleric
  • 301
  • 1
  • 3
  • 12

4 Answers4

44

Try like this.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"secondViewController") as! UIViewController
self.present(viewController, animated: true)    
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • 1
    Thanks. It's close, but I'm getting one error. Any suggestions? – Aleric Sep 30 '16 at 09:09
  • @Aleric I have never told you to remove this line `let storyboard = UIStoryboard(name: "Main", bundle: nil)` check the edited answer. – Nirav D Sep 30 '16 at 09:40
  • Welcome mate :) – Nirav D Sep 30 '16 at 17:56
  • @NiravD: will this crash the app if there isn't a viewController with that ID in the storyboard? –  Jun 10 '17 at 08:47
  • @3000 Yes that obvious – Nirav D Jun 10 '17 at 08:47
  • @NiravD: and should your code consider this possibility? –  Jun 10 '17 at 08:48
  • 1
    @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 app – Nirav D Jun 10 '17 at 09:03
  • @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 :-) –  Jun 10 '17 at 15:17
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
GoIn Su
  • 157
  • 1
  • 2
3

It's worked for me by this:

let vc = UIStoryboard(name: "ZWLStaticTabVc", bundle: nil).instantiateInitialViewController()
self.navigationController?.pushViewController(vc!, animated: true)
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.

Faizyy
  • 353
  • 2
  • 15