Cocoa - figuring out when NSTabView changed its tabs
My original answer suggested observing selectedTabViewItem
of NSTabView
, but that doesn't seem to work (when testing, I can get it to observe NSKeyValueObservingOptionInitial
).
Perhaps a smarter solution is to use a delegate. Pour tabView:didSelectTabViewItem:
into the appropriate controller.
Docs here .
source to share
Here's an example in Swift 3.
Create your own class for NSTabViewController
that acts like a delegate NSTabView
. The class NSTabViewController
already implements the protocol NSTabViewDelegate
.
class CustomTabViewController: NSTabViewController {
override func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
let identifier = tabViewItem?.identifier as? String
print(identifier)
}
}
Then in Interface Builder:
- Assign the custom class you created to your tab controller in the identity inspector in the right pane.
- In the interface hierarchy pane on the left, drag a control from the tab window to the tab view UI (the name will depend on your custom class) and select
delegate
from the little popover that appears
You can also implement other methods in your dedet as described in the documentation NSTabViewDelegate
.
source to share