Reputation: 926
I am wondering how I can override the notification being sent when the selection within a NSOutlineView
| NSTableView
has been changed. What I need to achieve is to add userInfo data to the notification being sent out.
I know that outlineViewSelectionDidChange
of a NSOutlineView
can be used to send out a custom notification like this:
func outlineViewSelectionDidChange(_ notification: Notification) {
NotificationCenter.default.post(name: NSOutlineView.selectionDidChangeNotification, object: self, userInfo: ["test": 1])
}
However, when I connect an observer to this notification ...
NotificationCenter.default.addObserver(
self,
selector: #selector(onSelectSetViewController(_:)),
name: NSOutlineView.selectionDidChangeNotification,
object: nil
)
... , I receive this as an output:
@objc
private func onSelectSetViewController(_ notification: Notification) {
print(notification)
}
name = NSOutlineViewSelectionDidChangeNotification, object = Optional(), userInfo = Optional([AnyHashable("test"): 1])
name = NSOutlineViewSelectionDidChangeNotification, object = Optional(), userInfo = nil
So the notification is being sent twice. How can I avoid this and sent out a notification with userInfo data only once?
Upvotes: 0
Views: 241
Reputation: 3847
Just change the name of your forwarding notification to something else and your notification will be sent only once. Then add an observer to listen to the forwarding notification. That will solve your problem:
func outlineViewSelectionDidChange(_ notification: Notification) {
NotificationCenter.default.post(name: MySelectionDidChangeNotification, object: self, userInfo: ["test": 1])
}
Then add this observer:
NotificationCenter.default.addObserver(
self,
selector: #selector(onSelectSetViewController(_:)),
name: MySelectionDidChangeNotification,
object: nil
)
Upvotes: 1