Inherit from EPUBNavigatorViewController
?
#407
-
This looks easy enough with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I wouldn't rely on inheritance when it's not explicitly designed for in the toolkit. Inheritance can be tricky and lead to hard-to-find bugs if you override things incorrectly. So it's preferable to use composition over inheritance. Here's the recommended way to catch events from the responder chain, such as a highlight event: class ReaderViewController<N: UIViewController & Navigator>: UIViewController {
let navigator: N
init(navigator: N) {
self.navigator = navigator
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
addChild(navigator)
navigator.view.frame = view.bounds
navigator.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(navigator.view)
navigator.didMove(toParent: self)
}
@objc func highlightSelection() {
}
} var editingActions = EditingAction.defaultActions
editingActions.append(EditingAction(
title: "Highlight",
action: #selector(ReaderViewController.highlightSelection)
)
let vc = ReaderViewController(navigator: EPUBNavigatorViewController(
publication: publication,
initialLocation: locator,
config: EPUBNavigatorViewController.Configuration(
editingActions: editingActions
),
)) I know this may not be the answer you expected, but I believe it's for the good of your codebase. Note that with this technique you will be able to reuse the highlighting code for all the navigators supporting it (PDF, at some point) as you don't subclass individual navigator classes. |
Beta Was this translation helpful? Give feedback.
I wouldn't rely on inheritance when it's not explicitly designed for in the toolkit. Inheritance can be tricky and lead to hard-to-find bugs if you override things incorrectly. So it's preferable to use composition over inheritance.
Here's the recommended way to catch events from the responder chain, such as a highlight event: