Re: File's Owner
Re: File's Owner
- Subject: Re: File's Owner
- From: mmalc crawford <email@hidden>
- Date: Sun, 25 May 2008 03:20:59 -0700
On May 25, 2008, at 12:15 AM, Johnny Lundy wrote:
And, if I don't understand something, I will ask why.
Here are some suggestions as to how you might pose your questions:
<http://www.catb.org/~esr/faqs/smart-questions.html>
This is not magic - there is actual computer code behind that File's
Owner concept, and it is deterministic, not vague, not abstract, not
a philosophical enigma, not random, not ambiguous. If I had the
source code I could see what it does.
But that's where you're leading yourself astray -- there isn't any
source code to see. The nib file is an object graph with a hole in
it. The File's Owner is the hole -- the one thing that *isn't*
created in the nib.
This is a *very rough* approximation:
@interface MyNibManager : NSObject
{
}
- (void)createNibForWindowWithButton:(NSString *)fileName;
- (void)loadNibForWindowWithButton:(NSString *fileName) withOwner:
(id)filesOwner;
@end
@implementation MyNibManager
- (void)createNibForWindowWithButton:(NSString *)fileName {
NSRect contentRect = NSMakeRect(100, 200, 400, 300);
NSWindow *aWindow = [[NSWindow alloc] initWithContentRect:contentRect
styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
NSRect buttonFrame = NSMakeRect(10, 20, 80, 20);
NSButton *aButton = [[NSButton alloc] initWithFrame:buttonFrame];
[aButton setButtonType:NSMomentaryLightButton];
[aButton setTitle:@"Push Me"];
[aButton setAction:@selector(myAction:)];
[aButton setTarget:(id)-1]; // pretend kludge to represent File's Owner
// Notice that there's no code for File's Owner itself
[[aWindow contentView] addSubview:aButton];
NSArray *tloArray = [NSArray arrayWithObject:aWindow];
NSDictionary *nib = [NSDictionary dictionaryWithObject:tloArray
forKey:@"TopLevelObjects"];
[NSKeyedArchiver archiveRootObject:nib toFile:fileName]
}
- (void)loadNibForWindowWithButton:(NSString *fileName) withOwner:
(id)filesOwner {
NSDictionary *nib = [NSKeyedUnarchiver
unarchiveObjectWithFile:fileName];
NSArray *tloArray = [nib objectForKey:@"TopLevelObjects"];
[filesOwner setWindow:[tloArray objectAtIndex:0]];
NSTextField *aButton = [[[window contentView] subViews] objectAtIndex:
0];
[filesOwner setButton:aButton];
if ([button target] == (id)-1) {
[button setTarget:filesOwner];
}
}
@end
@interface MyFilesOwner : NSObject
{
IBOutlet NSWindow *window;
IBOutlet NSButton *button;
}
@property NSWindow *window;
@property NSButton *button;
- (IBAction)myAction:sender;
@end
@implementation MyFilesOwner
@synthesize window, button;
- (IBAction)myAction:sender {
NSLog(@"Button pressed");
}
- (void)dealloc {
[window release];
[button release];
[super dealloc];
}
@end
int main(int argc, char *argv[])
{
NSString *fileName = [NSHomeDirectory()
stringByAppendingPathComponent:@"MyInterface.mnib"];
MyNibManager *myNibManager = [[MyNibManager alloc] init];
[myNibManager createNibForWindowWithButton:fileName];
MyFilesOwner *filesOwner = [[MyFilesOwner alloc] init];
[myNibManager loadNibForWindowWithButton:fileName ithOwner:filesOwner];
// ...
}
Despite teaching OB/GYN for 17 years, this is why computer science
is always my main interest.
Perhaps it's your background that's causing you the problem. My
father was Dr. J. Selwyn Crawford. I persuaded him to buy a Mac in
1986. He somehow failed to understand (a rarity) that when you use a
word processor you don't have to put Returns at the end of the line...
I've written firmware before we called it firmware. I have never NOT
been able to grasp something until this and bindings. Aaron says
lots of people have trouble understanding File's Owner, so I can
only conclude that it's the documentation, or lack thereof.
I think this is a fallacious conclusion. There are numerous
descriptions of File's Owner, and people typically read many of them.
My perspective, after teaching this for some time, is that some people
simply have difficulty understanding that it's as easy as it is. It's
got to be more complicated...
It isn't.
Interface Builder is a WYSIWYG object graph editor.
A nib file is an archived object graph.
File's Owner is a place-holder for an object created outside of the
nib that is made the "owner" of the object graph when the object graph
is unarchived (i.e. when the nib file is loaded).
When the nib file is loaded, any connections you made between objects
in the object graph and the File's Owner are reestablished using the
object set to be the owner (typically in -loadNibNamed:owner:).
mmalc
_______________________________________________
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