Question

I would like to link a tab of a OS X Cocoa Tab View to a specific action, but ctrl+drag doesn't work.
I found an other post of somebody that had about the same problem than I do, but it was on iOS and I am failing to adapt this to OS X. I don't understand if I am failing because I am not using the good functions or if it is for another reason.

Here is what I am doing when trying to adapt the method given on the other post : enter image description here

First, I ctrl+drag from the Tab View to "File's Owner" and make it the delegate of this.

Then, I add this code to AppDelegate.h :

- (void)tabBar:(NSTabView *)tabBar didSelectItem:(NSTabViewItem *)item  

And on AppDelegate.m :

- (void)tabBar:(NSTabView *)tabBar didSelectItem:(NSTabViewItem *)item
{
    if([item.identifier isEqualTo:@1])
    {
        NSLog(@"Click !");
    }
}

The identifier of my tab "Apprendre" is 1.

The problem is that when I run this code, nothing appears in the console when I click on the "Apprendre" tab. Do you know where I am wrong ?

Was it helpful?

Solution

You have used wrong method.

- (void)tabBar:(NSTabView *)tabBar didSelectItem:(NSTabViewItem *)item

Instead it should be

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem

This is very straight forward.

Hook the NSTabView to the AppDelegate for delegate. (Be sure that you hook correctly to the TabView itself not its parts)

enter image description here

Then check for the Identifier and Label for each of the tabs: enter image description here

Then the delegate method goes like this:

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem{

    if ([tabViewItem.identifier isEqualToString:@"1"]){
        NSLog(@"ONE");
    }
    else{
        NSLog(@"TWO");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top