How can I find out when a NSTabViewItem has been changed, i.e. a user has changed the view of an NSTabView?
Ideally I want to generate a notification but any solution would be welcome.
How can I find out when a NSTabViewItem has been changed, i.e. a user has changed the view of an NSTabView?
Ideally I want to generate a notification but any solution would be welcome.
My original answer suggested to observe selectedTabViewItem
of NSTabView
, but that doesn't seem to work (on testing I can only get it to observe NSKeyValueObservingOptionInitial
).
A probably smarter solution is to use a delegate. Implement tabView:didSelectTabViewItem:
in the relevant controller.
Docs here.
Here is an example in Swift 3.
Create a custom class for your NSTabViewController
which acts as delegate of NSTabView
. The NSTabViewController
class already implements the NSTabViewDelegate
protocol.
class CustomTabViewController: NSTabViewController {
override func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
let identifier = tabViewItem?.identifier as? String
print(identifier)
}
}
Then in Interface Builder:
delegate
from the little popover that appearsYou can also implement other methods in your delegate as explained in the documentation of NSTabViewDelegate
.