Cocoa - figuring out when NSTabView changed its tabs

how to know when an NSTabViewItem has changed, i.e. the user changed the NSTabView view.

Ideally, I want to generate a notification, but any solution would be welcome.

Thank!

+3


source to share


2 answers


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 .

+4


source


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

.

0


source







All Articles