Cocoa delegate nstabviewcontroller

I have a basic NSTabViewController with several 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 every NSViewController if the hasChanges variable is true , then a message will appear:

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

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

If I try to use NSTabViewDelegate in my MyViewController , then I do not know how to delegate MainTabViewController in it. Where to attach it?

 class MyViewController: HIDNSViewController { func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) { //Never called } } 
+5
source share
1 answer

0x101e17a10 is the address of the NSTabView instance NSTabView to by the tabView object. You need to print tabView identifiers in your didSelect method.

You need to set the identifiers in 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") } } 

Tab output:

enter image description here

+1
source

Source: https://habr.com/ru/post/1258849/


All Articles