Re: Interface builder, .nibs, etc ...
Re: Interface builder, .nibs, etc ...
- Subject: Re: Interface builder, .nibs, etc ...
- From: Chuck Soper <email@hidden>
- Date: Tue, 16 Dec 2003 21:51:55 -0800
I discovered the view controller approach from Glen Fisher's message:
http://cocoa.mamasam.com/COCOADEV/2002/05/2/34310.php
not by reading Rainer Brockerhoff's article, but it is a good article.
Chuck
Date: Tue, 16 Dec 2003 21:12:12 -0800
Subject: Re: Interface builder, .nibs, etc ...
To create a view with various controls that can be instantiated
multiple times, I create a nib whose file owner is a subclass of
NSWindowController. This way I have the view in its own window,
NSTabViewItem, NSBox, or other sub-views. To add it as a view you
have to set the window to nil in the awakeFromNib message in the
nib. I discovered this approach after reading Rainer Brockerhoff's
article on plug-ins: http://cocoadevcentral.com/articles/000068.php
I like being able to test the interface in IB to make sure
auto-sizing is correct for all of the controls. If it was in a
custom view I don't think I'd be able to test the interface in IB.
Below is code to load such a nib into an NSTabViewItem. One concern
I have is that my application may end up having twenty or thirty
nibs. Is there anything wrong with that? Will localization be
easier, harder, or the same? I haven't tried releasing the nib's
memory yet, but I think that as long as I implement dealloc in the
file's owner it should be okay.
Does anyone have comments or suggestions about this approach?
Thanks, Chuck
//myWindowController is a subclass of NSWindowController
NSString * theNibName = @"NibFileName";
myWindowController = [[myWindowController alloc]
initWithWindowNibName:theNibName];
mainView = [[[myWindowController window] contentView] retain];
[myWindowController setWindow: nil];
NSTabViewItem* tabItem = [[[NSTabViewItem alloc]
initWithIdentifier:nil] autorelease];
NSRect mainFrame = [tabView contentRect];
[mainView setFrame:mainFrame];
[tabItem setView:mainView];
[tabItem setLabel:theNibName];
[tabView addTabViewItem:tabItem];
At 5:21 PM -0800 12/16/03, Dustin Voss wrote:
On 16 Dec, 2003, at 1:24 PM, Stiphane Sudre wrote:
Just drag a custom view instead of a window. And it will be your
root widget that you will be able to add dynamically to the
Inspector Window.
To expand on this answer, I have built view controllers analogous
to window controllers, where the view controller is the owner of a
view-only NIB. I only need to use this technique because I put the
view in its own NIB and make multiple instances of it
programatically. If you only need one instance of the view, it'd be
easier to put in in the inspector window's NIB.
I use NSNib to pre-load the NIB and instantiate it as many times as
I need to. Each time I instantiate it, I use a new controller
object as the file owner. NSNib sets all the outlets of the
controller object. One of the outlets is for the view itself. I use
that outlet to add the view to its superview.
The only problem I've found is in managing the lifetime of the
NIB's top-level objects. Apparently, once the view is added to its
superview, the view controller can no longer release it, and I get
a (seemingly harmless) malloc error. It is curious, and I worry
about memory leaks.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.