 |
 |
Help with dynamic tabs / Loading nibs
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Location: Brooklyn, NY
Status:
Offline
|
|
Hey everyone, i'm a n00b here, and just have a little design/implementation question...
I'm creating an app that makes NSTabViewItems on the fly, with each tab being similar in layout. (An NSOutlineView, a few buttons etc)
I've read a few posts here that the best way to duplicate the layout of the tab would be to use a seperate nib file and load it when it is needed.
I can make an empty tab and have it show.
I can load the nib, but it doesn't show, and I have no idea what to do.
Here is where I run into trouble:
In IB I made the layout a subclass of NSView, which was the only object that could start a new view besides a window or a panel.
(Basically I dragged a custom view from the palette and built on top of that)
I want these other objects to appear inside a tab that is already situated on a window...
Upon reading the docs for NSView, it says I'd have to implement my own drawRect and initWithFrame methods. It also says that I *could* use some object that already knows how to draw itself (a la NSBox, which if I could have used, I would have)
Is there some other object I can build on, that will show up rather easy?
I know I could also use a Coder, but I'd rather use an external nib file.
I was thinking about looking into how a Preference pane is built, since that is a nib that is loaded into an existing window.
Thanks in advance, and any help is appreciated
(Btw this is my first Objective-C program)
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by KaosDG:
<strong>I can load the nib, but it doesn't show, and I have no idea what to do. </strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">What do you mean "it doesn't show"? nibs don't have graphical representations in running apps. You'll need an outlet to your view instance, and then you can use -[NSTabViewItem setView:] to associate it with its tabviewitem.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"><strong>In IB I made the layout a subclass of NSView, which was the only object that could start a new view besides a window or a panel.
(Basically I dragged a custom view from the palette and built on top of that)</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">A "Custom View" isn't actually a subclass of NSView, it's just a freeze dried NSView with lots of subviews already on it.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"><strong>I want these other objects to appear inside a tab that is already situated on a window...
Upon reading the docs for NSView, it says I'd have to implement my own drawRect and initWithFrame methods. It also says that I *could* use some object that already knows how to draw itself (a la NSBox, which if I could have used, I would have)</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Not really with you here. You want to add objects to a different tab? What does that have to do with overriding drawRect and initWithFrame?
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Location: Brooklyn, NY
Status:
Offline
|
|
ok let me see if I can unconfuse myself as well...
My app is simply a button, and a tabview...
I press the button, and it makes a new tab.
(It's nothing functional, just practicing with Cocoa/Obj-C for now)
Creating the new tab goes fine... they pop up, but they are emtpy.
I want to load the tab's objects from a nib file, a few buttons, some outline views.. (they won't have any functions yet, i just want them to show up)
I made the basic layout for the tabs in IB, using a Custom View set to NSBox.
Just a surrounding box with the buttons and outline that I want to show up on the newly created tab.
my code for creating the tab (and attempting to have the nib load/show) looks like this:
(from my button controller)
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> NSTabViewItem *newTab = [[NSTabViewItem alloc] init];
NSBox *myBox = [[NSBox alloc] init];
[newTab setLabel:@"Test?"];
[myTabview addTabViewItem:newTab];
if(! ([NSBundle loadNibNamed:@"test" owner:myBox]) )
{
//Failed load
NSLog(@"nib load failed."  ;
}
else
{
NSLog(@"nib load succeeded."  ;
[newTab setView:myBox];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">When I run it and click the button, the tab is created, the nib file loads OK (the console shows my message)
but the TabViewItem just shows a default box (no buttons or anything in it)
so...
am I retarded or something?
Is my thinking off? I thought loading the nib would make myBox contain all of the objects that are on it...
did I miss a step?
is this even possible? 
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Location: Brooklyn, NY
Status:
Offline
|
|
A few things I noticed as I mess with the above code...
If I try to print out myBox's superview/window (using NSLog), they are null even though I used the viewWillMoveToWindow and viewWillMoveToSuperview methods on them...
any one?
I am determined to conquer this beast!

|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status:
Offline
|
|
I think the key thing you're missing is that you need to have an outlet that will point to the new view when you load it from the nib file. So, in your controller (the object that responds to your button's action message), you should declare an outlet:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">@interface MyController : NSObject
{
IBOutlet NSView *nibLoadedView;
//whatever else you have
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">After you've done that and saved the header file, open up your test.nib and drag the controller's header file into the nib window. IB will parse the file and see that you have that outlet declared.
Then, click on the file's owner icon, go to the inspector, and choose your class from the list. This tells IB that the owner of the nib file (i.e. the second argument passed into loadNibNamed:owner  is an instance of your controller class.
Create your custom view, leaving it as a plain NSView, and put all your stuff inside. Then make a connection from the file's owner icon to the custom view and select the outlet that you created.
Then, in your code, you do this:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">@implementation MyController
-(IBAction)myButtonAction  id)sender
{
NSTabViewItem* newTabViewItem = [[NSTabViewItem alloc] init];
if([NSBundle loadNibNamed:@"test" owner:self])
{
[newTabViewItem setView:nibLoadedView];
[nibLoadedView release];
nibLoadedView = nil;
//set title, add to tab view, whatever else
}
}
@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Location: Brooklyn, NY
Status:
Offline
|
|
w00t!
Thank you VERY much.
I kinda figured that it would have somethign to do with the outlets/file owner bizmiss, but all the docs/books/threads I've seen have dealt with windows/panels only...
THANK YOU.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
bewebste has hit the nail on the head here.
Depending on how you're going to place items from the newly loaded nib into the existing tab view you may also find NSView's addSubview method useful.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|