Re: accessing view in a nib
Re: accessing view in a nib
- Subject: Re: accessing view in a nib
- From: Christopher Corbell <email@hidden>
- Date: Thu, 24 Jul 2003 10:38:01 -0700
Here's the 10-step system that I use for loading nib-based views. This
approach
does not require a special NSView subclass (though you can still
subclass
NSView if desired). You also don't have to worry about setting
intermediate
flags etc. as you just create an instance of your view's controller and
once
it's done your view is guaranteed to be loaded and put into the
superview
you define.
1) Create an empty nib
2) Create the view in the nib (using Custom View) including subviews
etc.
3) Create a new subclass of NSObject to be your view's controller. It
should
include an IBOutlet for the view. I'll call this class
MyViewController, and the
NSView outlet m_view.
4) Change the "Custom class" setting for the nib file's owner to
MyViewController
- your nib now contains an instance of your view, and an instance of
your controller (as the file's owner).
5) Connect the m_view outlet of the file's owner instance to your
custom view.
6) Generate the files for the MyViewController class
7) Save the nib file as MyViewController.nib
8) In MyViewController header, add the instance init method:
- initWithSuperView:(NSView *)superview;
9) Define this in the source:
- (id) initWithSuperview:(NSView *) superview
{
self = [super init];
if(self)
{
// Load nib, using this object as the file's owner...
if([NSBundle loadNibNamed:@"MyViewController" owner:self])
{
// By the time the nib is loaded, view's awakeFromNib will
// have been called and m_view outlet set in this object...
// Add m_view to superview
[superview addSubview:m_view];
// do whatever other initialization you need here
}
else
{
// nib loading failed - this object will be invalid, autorelease &
raise exception
[self autorelease];
self = nil;
[NSException raise:@"MyViewController-init-exception"
format:@" - could not load nib from bundle"];
}
}
return self;
}
10) Add creation of this view to the appropriate place in your app
code, e.g.:
#import "MyCustomView.h"
// ...
- (MyCustomViewController *) loadMyCustomView:(NSWindow *) hostWindow
{
return [[MyCustomViewController alloc] initWithSuperview:[hostWindow
contentView]];
}
You may need to use a more particular view as the superview for the one
you're loading; often I'll have an empty custom view in the window's
nib act
as the superview for the custom view, and the custom view will adopt the
superview's size and bindings. Another variation that I've used is to
have
the same controller instance be capable of reloading it's m_view from
alternate nib files based on some setting - this is a pretty easy
modification
to make, just move the nib-loading stuff to a method called reload().
Just
cache the old m_view and reload the nib, then use the superview's
replaceSubview method to replace the old m_view with the new one.
Hope this helps, I found it really useful when someone showed me how
to make a controller class the nib file's owner, and load itself this
way!
- Christopher
On Wednesday, July 23, 2003, at 11:05 PM, Francisco Tolmasky wrote:
Ok, so I'm trying to implement a more modularized preference system,
and I'd like to have each little preference pane in its own nib, so as
to not need to load them all at once or whatever. Anyways, how do I
make class that will load from a nib as NSWindowControllers do. I
just want the file's owner to be an instance of said class, and to
have a view along with it. I really don't want to have to make it a
subclass of nswindowcontroller. Also, how can I tell when the nib is
loaded and thus ready to be shown in the preferences thing. Just
awakeFromNibbing some flag or something?
Thank you,
Francisco Tolmasky
email@hidden
http://users.adelphia.net/~ftolmasky
_______________________________________________
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.
_______________________________________________
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.