Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically initialize ViewController

I am trying to initialise a UIViewController in my Swift application but I am facing a problem to which I cannot find any definitive answer.

I would like to call this from a FlowCoordinator to initialise the controller, but my initialiser requires a NSCoder object due to the required init?(coder: NSCoder) function.


MyAwesomeController()

Is there a way to initialise differently the controller, without the need to pass the NSCoder object?

If there is not, how can I create such an object in a way to avoid the following exception:

'NSInvalidArgumentException', reason: '*** -decodeObjectForKey: cannot be sent to an abstract object of class NSCoder: Create a concrete instance!'

Thank you very much in advance

like image 652
modusT Avatar asked May 11 '19 11:05

modusT


1 Answers

Use something like this, I included a property also as a demo:

class MyAwesomeViewController: UIViewController {
    let someInt: Int

    init(someInt: Int) {
        self.someInt = someInt

        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("Storyboard are a pain")
    }
}

I like creating everything programmatically.

like image 187
J. Doe Avatar answered Sep 28 '22 18:09

J. Doe