3

I want to know when a NSTabView switched to a particular view. For that I've extended NSTabViewController with my custom class to be able to act as delegate:

class OptionsTabViewController: NSTabViewController {
    override func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
        print(tabViewItem!.identifier)
    }
}

This prints what looks like pointer memory positions:

Optional(0x608000100e10)
Optional(0x6080000c36b0)

I imagine it should be possible to set those identifiers somewhere in interface builder, but I've tried writing stuff in different text fields labeled as identifier and still get those memory position values in the console.

I've also used print(tabViewItem!.label) but it prints the label in the tab button.

So how can I set that identifier to be able to recognise which view is active in the tab view component?

Pier
  • 10,298
  • 17
  • 67
  • 113

1 Answers1

5

First of all, you may define your identifier(s) in this way:

enter image description here

then in your code you might define an enum for checking which is the current selected tab, doing something like:

enum YourTabs:String {
   case tab1
   case tab2
   case none
}

class ViewController: NSViewController, NSTabViewDelegate {
    @IBOutlet var tabView:NSTabView!

    public func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
        if let identifier = tabViewItem?.identifier as? String,
            let currentTab = YourTabs(rawValue: identifier) {
            switch currentTab {
            case .tab1:
                print("do something with tab1")
                break
            case .tab2:
                print("do something with tab2")
                break
            default:
                break
            }
        }
    }
}
mugx
  • 9,869
  • 3
  • 43
  • 55
  • Parameter `tabViewItem` is the tab view item that was selected. Use `tabViewItem` instead of `tabView.selectedTabViewItem`. – Willeke Jan 22 '18 at 11:06
  • Thanks. The image you posted is in the attributes inspector which I completely missed since I was looking at the identity inspector. – Pier Jan 22 '18 at 11:53