Arti Arti - 1 month ago 19
Swift Question

Cocoa delegate nstabviewcontroller

I have a main

NSTabViewController
with few tabs:

class MainTabViewController: NSTabViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}

override func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
print(tabView)// returns <NSTabView: 0x101e17a10> but what to do with it ?
}

}


I want to check in every
NSViewController
if variable
hasChanges
is
true
then pop up message:


"You have unsaved changes. Do you want to change tab ?"


If i check this in
MainTabViewController
i get
<NSTabView: 0x101e17a10>
and what to do with it i don't know.

If i try to use
NSTabViewDelegate
in my
MyViewController
then i don't know how to delegate
MainTabViewController
in it. Where attach it ?

class MyViewController: HIDNSViewController {
func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
//Never called
}
}

Answer

0x101e17a10> is the ID of your object of NSTabView. You need to print the tabView identifiers within your didSelect method.

You need to set the identifiers in the Interface Builder:

enter image description here

And then (for example):

override func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
    if tabView.selectedTabViewItem?.identifier! as! String == "1" {
        print("FIRST VC")
    } else {
        print("SECOND VC")
    }
}

Output when tabbing:

enter image description here

Comments