So, I'm now in the mood to try this again.
I think for any app it's reasonable that a UIViewController should be able to perform actions once it's visible, even if it's not first responder. Right now, this is not the case.
So what you're suggesting is that I do something like this. This really overcomplicates app logic when each UIViewController could perfectly handle their own actions, if only the system would ask each visible view controller if it can perform them.
Now I have to implement functions for each menu action at the top level, then check if the action should be enabled and then find the responsible view controller myself and tell it to execute the function. And this is just the basic implementation. What if I want to determine if a menu action should be enabled/disabled based on state in a specific view controller? Then I have to do this manually too. Also, all my view controllers functions need to be public then. I'm really not sure this is a thought-through approach.
@objc
func test() {
let gamesViewController = ...
gamesViewController.test()
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(test) {
if let a = self.selectedViewController as? UINavigationController {
if a.viewControllers.last is GamesViewController {
return true
}
}
}
return false
}
Even the Apple sample project "Adding Menus and Shortcuts to the Menu Bar and User Interface" struggles with this, as the two actions for the PrimaryViewController do not work unless you add an item and then select it to make it the first responder.