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.
selectedTabViewItem
in your view. Then you can either observe the controller property or do something useful in its setter method. –
Valentinevalentino delegate
of a tabView
: *** Assertion failure in -[NSTabView setDelegate:], Failed to set (contentViewController) user defined inspected property on (NSWindow): A TabView managed by a TabViewController cannot have its delegate modified –
Redd 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
.
© 2022 - 2024 — McMap. All rights reserved.