Re: File's Owner
Re: File's Owner
- Subject: Re: File's Owner
- From: Scott Ribe <email@hidden>
- Date: Fri, 23 May 2008 21:01:17 -0600
- Thread-topic: File's Owner
OK, maybe an example will help. Jason's point about File's Owner having more
outlets and "more to do" in nibs other than MainMenu.nib is good.
Assume you're writing some kind of database app. Assume you're creating a
nib for the "contact entry" screen. Assume a nib for this screen with
various controls on it, and a class for this screen that starts off like
this:
@interface ContactWindowController : NSWindowController
{
IBOutlet id firstNameTxt;
IBOutlet id lastNameTxt;
IBOutlet id addressTxt;
IBOutlet id phoneTxt;
IBOutlet id commentsTxt;
IBOutlet id saveBtn;
}
...
@end
So what do you want to do here? Well, you want to open a window based on the
nib, with all those controls displayed. And you want an instance of your
class. And you want your class members hooked up to those controls that are
displayed. Right? So how to do this?
Well, first, create the nib, then set the class of File's Owner to
ContactWindowController, then wire up those outlets of File's Owner to
firsNameTxt and so on. Also, set the File's Owner "window" outlet (inherited
from NSWindowController) to the window, and set the window's delegate to
NSWindowController (not used in this example, but required for any real
use).
Next, add this method to ContactWindowController:
- (id) init
{
[super initWithWindowNibName: "newcontact.nib" owner: self];
}
Now, call the following somewhere in your application (probably in a method
of your app delegate, probably wired up to a menu item in MainMenu.nib):
ContactWindowController *wc = [[ContactWindowController alloc] init];
There you have it: wc is now an instance of ContactWindowController, and its
firstNameTxt et al instance variables all point to the controls in the
window which is set up with those controls. Sort of.
There's one subtlety in that much of the set up of the window is "lazy" and
doesn't actually happen until you ask for the window via the "window"
method. In other words, much of the magic doesn't happen until you call:
[wc window]
But this hardly matters because, except in unusual circumstances, the very
next thing you're going to do with your controller after creating it is:
[[wc window] makeKeyAndOrderFront: wc]
In order to actually display the window and bring it to the front.
And of course if you add the following method to your class
- (id) doSaveBtn: (id) sender
{
// save all the data to the database
// left as an exercise to the reader since it's totally trivial ;-)
}
And wire up the button to File's Owner doSaveBtn:, then when the use clicks
the save button, the method will be called, and the data will be saved. Or
not, depending on various minor details, like the state of the connection to
the database ;-)
--
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 722-0567 voice
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden