Apologies if this has been asked before, I've searched around a lot and many answers are from earlier Swift betas when things were different. I can't seem to find a definitive answer.
I want to subclass UIViewController
and have a custom initializer to allow me to set it up in code easily. I'm having trouble doing this in Swift.
I want an init()
function that I can use to pass a specific NSURL
I'll then use with the view controller. In my mind it looks something like init(withImageURL: NSURL)
. If I add that function it then asks me to add the init(coder: NSCoder)
function.
I believe this is because it's marked in the superclass with the required
keyword? So I have to do it in the subclass? I add it:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
Now what? Is my special initializer considered a convenience
one? A designated one? Do I call a super initializer? An initializer from the same class?
How do I add my special initializer onto a UIViewController
subclass?
They are documented here.
If you need a custom init for a popover for example you can use the following approach:
Create a custom init that uses the super init with nibName and bundle and after that access the view property to force the load of the view hierarchy.
Then in the viewDidLoad function you can configure the views with the parameters passed in the initialization.