I have two main screens in my app, currently both just subclasses of UIViewController
. These two view controllers are very similar - they both implement my custom subclass of UIView
called HeaderView
that is responsible for displaying information and taking user input. As it stands, this code is repetitive because the HeaderView
setup is the same for both view controllers - the only difference is what happens when the user confirms the text entry in HeaderView
.
To cut down on repetitive code, I am creating a class called InputViewController
(a subclass of UIViewController
) that houses the aspects of the two view controllers that are identical. Eventually, I want the two view controllers to subclass InputViewController
instead of UIViewController
.
class InputViewController: UIViewController, InputProtocol {
private let headerView = HeaderView()
override func viewDidLoad() {
super.viewDidLoad()
// layout, etc.
setupCallbacks()
}
internal func setupCallbacks() {
headerView.onUpdate = { (text: String) in
// called when user confirms text entry in headerView
self.onHeaderUpdate()
}
}
internal func onHeaderUpdate() {} // Blank function
}
setupCallbacks()
and onHeaderUpdate()
are methods defined in the protocol that the InputViewController
conforms to. The HeaderView
implements a callback closure that is handled in setupCallbacks()
by headerView.onUpdate...
The protocol that InputViewController
conforms to:
protocol InputProtocol {
func setupCallbacks()
func onHeaderUpdate()
}
To illustrate this, I drew up a diagram;
Since I want the subclasses of InputViewController
to override the onHeaderUpdate()
method, is it conventional to leave the definition of onHeaderUpdate()
in InputViewController
blank or is there another solution to this?