Simple scenario: I have a base ViewController with a method that I would like to optionally override in subclasses. The twist, is that I would like to do the override in an extension of the subclassed ViewController.
This does not work:
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
overrideStyles()
}
func overrideStyles() {
// do nothing here. Optionally overridden as needed
}
}
class ViewController: BaseViewController {
}
// this is in another file that is to a specific target
extension ViewController {
override func overrideStyles() {
print("YAY!")
}
}
Neither does it work with protocols:
protocol OverridesStyles {
func overrideStyles()
}
extension OverridesStyles {
func overrideStyles() {
print("default method that does nothing")
// do nothing, this makes the method optional
}
}
class BaseViewController: UIViewController, OverridesStyles {
override func viewDidLoad() {
super.viewDidLoad()
overrideStyles()
}
}
// this is in another file that is to a specific target
extension ViewController {
func overrideStyles() {
print("YAY!")
}
}
I can't subclass the entire ViewController class because that's the class in the Storyboard and I can't have a different Storyboard per target.
Is there any other way to do this? The idea was that I could have different targets and a different extension per target because the overrideStyles method may reference different UI elements depending on the target.
Thanks.