Add a tab in Objective-C

Moderator
Posts: 624
Joined: 2007.09
Post: #1
I'm programming a Text Editor in Cocoa and Objective-C, and can't figure out how to add a new tab to my NSTabView. My goal is to have a "New" button that you can click to add a tab to the TabView. I have sublassed NSObject and made a Class called LGTabView with an outlet named newTab: and an action called addNewTab. So far, I have the "New" button linked to the LGTabView, and corresponding to the addNewTab action. I then have LGTabView hooked up to NSTabView, using my newTab outlet. My problem is, I don't know of a function to put into my action to make a new tab. In the xCode documentation, I found the addTabViewItem function, but I can't figure out how to use it. Any Suggestions?

Lincoln Green a.k.a Hairball183 alias Hairball600 alias Jiminy35 alias Lincoln Green.
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #2
Oh, here's how I was trying to use the function…(Please don't go down to your grave in agony yet, I need help)Rolleyes

Code:
#import "LGObject.h"

@implementation LGObject

- (IBAction)addNewTab:(id)sender
{
    [newTab addTabViewItem:(NSTabViewItem *)Document2]
}

@end
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
The fact that you needed a cast to pacify the compiler should be a big flashing red sign saying you screwed up Rasp

You need to create an NSTabViewItem. Use -initWithIdentifier:, -setLabel: and -setView:.
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #4
And how do you use that? This is what I have, but it doesn't make me a new tab.

Code:
- (IBAction)addNewTab:(id)sender
{
    NSTabViewItem *tabItem = [[NSTabViewItem alloc] initWithIdentifier:@"Document2"];
    [tabItem setLabel:@"tabLabel"];
    [newTab addTabViewItem:tabItem];
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Maybe it won't show up if you haven't set its content view? I dunno, looks reasonable to me.
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #6
How do you set it's content view?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
* OneSadCookie points up the thread.

Also, you should look up NSTabViewItem in Xcode's documentation window and read what it has to say...
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #8
I can't figure out how to use the setView statement. Off to check the documentation.
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #9
Alright, I get the following message in my runtime log when I click the button.

Code:
2008-01-18 12:04:13.389 CocoaNotePad[671] *** -[NSTextView addTabViewItem:]: selector not recognized [self = 0x335f60]
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #10
That means your newTab outlet points to an NSTextView and not an NSTabView.
Quote this message in a reply
Member
Posts: 53
Joined: 2007.08
Post: #11
Hairball183 Wrote:
Code:
2008-01-18 12:04:13.389 CocoaNotePad[671] *** -[NSTextView addTabViewItem:]: selector not recognized [self = 0x335f60]

That means NSTextView doesn't have a -addTabViewItem method. I think you want NSTabView.

Bah, Josh beat me to it
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #12
Ah, You're right! Thank you thank you thank you thank you!


Lincoln Green
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #13
I was trying to redo my outlets and I messed something up. Now, I'm getting this…
Code:
2008-01-19 17:37:51.643 CocoaNotePad[1491] *** -[NSTabView setLabel:]: selector not recognized [self = 0x335580]
Wacko
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #14
It means that NSTabView does not have a setLabel: method. NSTabViewItem does. Perhaps you meant that?
Quote this message in a reply
Moderator
Posts: 624
Joined: 2007.09
Post: #15
Well right now, I have my LGObject outlet connected to the NSTabView object. How do I hook it up to the NSTabViewItem?
Quote this message in a reply
Post Reply