1

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 ?

Community
  • 1
  • 1
Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64

1 Answers1

2

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");
    }
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • The `File's Owner` is `NSApplication` so the delegate should probably be `App Delegate` instead. – trojanfoe May 09 '14 at 10:01
  • @trojanfoe: I didn't get you. The above is working fine. – Anoop Vaidya May 09 '14 at 10:04
  • The OP's `File's Owner` is `NSApplication` not `AppDelegate`, so I'm not sure connecting to `File's Owner` is correct (for them at least). – trojanfoe May 09 '14 at 10:05
  • @AnoopVaidya Finally I solved my issue, I was simply not using the good method ! – Pop Flamingo May 09 '14 at 10:09
  • @trojanfoe I solved my problem, in fact I was not using the good method ! And it looks like you are right for the File's owner, because it works only if I connect it to App Delegate. – Pop Flamingo May 09 '14 at 10:10
  • @TrevörAnneDenise: Oh yaa, you have used **`TabBar`** not `TabView`. – Anoop Vaidya May 09 '14 at 10:11
  • @AnoopVaidya Thanks for your answer ! I suggest you to edit your post, because it works only when linking to App Delegate ! It doesn't work when I link it to File's owner. – Pop Flamingo May 09 '14 at 10:12
  • @TrevörAnneDenise: Yeah I noticed it. You hooked to file's owner. not to appDelegate. Also thanks to `trojanfoe`. – Anoop Vaidya May 09 '14 at 10:15