Push ViewController with coder aDecoder: NSCoder

2.3k views Asked by At

I'm trying to push a view controller using:

var vc2 = ViewController2()

self.navigationController?.pushViewController(vc2, animated: false)

but in my second view controller, I'm have:

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

But I get the error Missing argument for parameter 'coder' in call in the first ViewController. What goes in the parenthesis in the first view controller?

2

There are 2 answers

0
Icaro On BEST ANSWER

There are two ways you can fix this issue:

The easy way, just call the function passing nil to the parameters:

var vc2 = ViewController2(nibName: nil, bundle: nil)

The best way, create convenience initializers in your class:

class ViewController2: UIViewController {
    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }
    override init (frame : CGRect) {
        super.init(frame : frame)
    }
    convenience override init () {
        self.init(frame:CGRectZero)
    }
}

and now you can call:

var vc2 = ViewController2()
0
Andriy Gordiychuk On
class ViewController2: UIViewController {
    convenience init () {
        self.init(nibName: nil, bundle: nil)
    }
} 

Now you can call ViewController2()