Cocoa - finding out when an NSTabView has changed its tabs
Asked Answered
F

2

5

How can I find out when a NSTabViewItem has been changed, i.e. a user has changed the view of an NSTabView?

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

Forby answered 27/3, 2012 at 7:51 Comment(0)
V
5

My original answer suggested to observe selectedTabViewItem of NSTabView, but that doesn't seem to work (on testing I can only get it to observe NSKeyValueObservingOptionInitial).

A probably smarter solution is to use a delegate. Implement tabView:didSelectTabViewItem: in the relevant controller.

Docs here.

Valentinevalentino answered 27/3, 2012 at 8:5 Comment(8)
Do you mean like this: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (stvi:) name:@"selectedTabViewItem" object: nil ]; 'stvi' being the method being called.. In that case, it doesn't work, no notification is being posted.Forby
Thanks for the reply, unfortunately, I am getting the error 'NSTabView' may not respond to '-addObserver:forKeyPath:context:' Any ideas?Forby
Also, when you mention the bindings, to what would you normally bind the tab view? I am working on OS X, so the iOS issues is not a problem till now..Forby
Regarding bindings: you bind a property to another, so you could have a property in a controller object automatically change its value based on selectedTabViewItem in your view. Then you can either observe the controller property or do something useful in its setter method.Valentinevalentino
The program will compile now, but there is no visible effect, i.e. no message being displayed where you listed //Do your stuff here above when I change the tabs... Obviously I put in some code, in my case, to log the event happeningForby
Completely changed my answer - sorry for any confusion.Valentinevalentino
Thanks for the revision.. I'm new to delegates so I would need to research a little bit how to implement them.. I have been trying the bindings solution, however there is a problem that I cannot seem to overcome.. How can I bind to a method, rather than an object? In other words, I need to bind a value (say an integer?) to the method selectedTableViewItem. How would I go about doing that? Thanks a lot for your help Monolo!Forby
I am getting the following error when I try set the delegate of a tabView: *** Assertion failure in -[NSTabView setDelegate:], Failed to set (contentViewController) user defined inspected property on (NSWindow): A TabView managed by a TabViewController cannot have its delegate modifiedRedd
M
2

Here is an example in Swift 3.

Create a custom class for your NSTabViewController which acts as delegate of NSTabView. The NSTabViewController class already implements the NSTabViewDelegate protocol.

class CustomTabViewController: NSTabViewController {
    override func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
        let identifier = tabViewItem?.identifier as? String
        print(identifier)
    }

}

Then in Interface Builder:

  1. Assign the custom class you created to your Tab View Controller in the Identity Inspector in the right panel
  2. In the interface hierarchy panel on the left, control drag from the Tab View to the Custom Tab View Controller (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 delegate as explained in the documentation of NSTabViewDelegate.

Mors answered 22/1, 2018 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.