Swift 3 - How to instantiate a View Controller?

0

I need to work with a method with attributes that are in another ViewController but I can not do it. What I'm trying:

let notasViewController: NotasViewController()

And also:

let notasViewController: NotasViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotasViewController") as! NotasViewController

Always return EXC_BAD_ACESS or an error saying that my Storyboard does not contain this View Controller
Some help? Thanks!

    
asked by anonymous 24.02.2017 / 15:38

2 answers

2

It will basically be 2 steps to be done on the Storyboard:

  • In your Main.storyboard , select the desired ViewController and modify its class to NotesViewController.
  • Modify the Storyboard ID field, with the name of the identifier that you will use in the code, in the case of your code, you are using: "NotesViewController"
  • The settings look like this:

    The"Use Storyboard ID" box is not required to be checked, it is useful if you want to leave the "Restoration ID" with the same ID in the Storyboard.

    Once you have done this, go to the View Controller class that you want to have an instance of the NotasViewController class and use the same code you used:

    let notasViewController: NotasViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotasViewController") as! NotasViewController
    

    In this way you will have access to the methods of class NotasViewController through constant notasViewController .

        
    27.02.2017 / 17:55
    -1

    You can do this to create a new instance of the ViewController:

    let notasViewController = NotasViewController()
    
        
    26.02.2017 / 20:37