Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa delegate nstabviewcontroller

Tags:

swift

cocoa

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
    }
}
like image 307
Arti Avatar asked Nov 08 '22 07:11

Arti


1 Answers

0x101e17a10 is the address of the NSTabView instance pointed to by the tabView object. 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

like image 58
David Seek Avatar answered Nov 15 '22 04:11

David Seek