I\'m having issue for writing custom init for subclass of UIViewController, basically I want to pass the dependency through the init method for viewController rather than se
You can't use a custom initializer when you initialize from a Storyboard, using init?(coder aDecoder: NSCoder)
is how Apple designed the storyboard to initialize a controller. However, there are ways to send data to a UIViewController
.
Your view controller's name has detail
in it, so I suppose that you get there from a different controller. In this case you can use the prepareForSegue
method to send data to the detail (This is Swift 3):
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "identifier" {
if let controller = segue.destinationViewController as? MemeDetailVC {
controller.meme = "Meme"
}
}
}
I just used a property of type String
instead of Meme
for testing purposes. Also, make sure that you pass in the correct segue identifier ("identifier"
was just a placeholder).