Re: cocoa design for a database application
Re: cocoa design for a database application
- Subject: Re: cocoa design for a database application
- From: Quincey Morris <email@hidden>
- Date: Sun, 11 Jan 2009 10:35:50 -0800
On Jan 11, 2009, at 09:52, email@hidden wrote:
What I want to do is have a window that I can click on a button that
says "new person" and I want it to open a window (from a separate
nib file) with text fields for entering a first and last name. I
created a separate Window nib file (PersonWindow.xib) for this new
window. I put a NSObjectController (PersonController) in that nib
file that I will use for the bindings. What do I need to do in the
new person button action to tell it to create and open the window
for this nib file?
Looking through the documentation made me think I needed a
NSWindowController so I tried this, but I'm fairly certain it's not
correct.
Your idea of creating a window controller is a good one, but you're
going to need to create a custom subclass of NSWindowController
because (at least) the window controller needs to know what person its
window is associated with (so that it can supply the correct person
properties to bindings).
-(IBAction)newPressed:(id) sender {
NSLog(@"newPressed");
Person *person = [[Person alloc] init];
person.firstName = @"blank";
PersonController *pc = [[PersonController alloc]
initWithContent:person];
Don't create the object controller in code. Putting it in the XIB file
causes the object to be created for you, so it's a mistake to create a
second one in code. Normally, you should have no need to reference the
object controller in your code -- it's really just a piece of glue in
the XIB for connecting views to File's Owner.
NSWindowController *wc = [[NSWindowController alloc]
initWithWindowNibName:@"PersonWindow" owner:pc];
The "owner" parameter is the object that will be used as File's Owner
for the XIB file. That object is almost certainly going to be the
window controller itself, and almost certainly not an object
controller. The correct value for the owner parameter is "self" -- and
there's a shorter initWithWindowNibName method that defaults to that
correct value.
NSWindow *window = [wc window];
[window display];
Since you have the window controller right there, the easiest way to
display the window is -[NSWindowController showWindow:];
}
Here's how I'd re-write your above code:
-(IBAction)newPressed:(id) sender {
Person *person = [[Person alloc] init];
person.firstName = @"blank";
MyPersonWindowController *wc = [[MyPersonWindowController alloc]
initWithPerson: person];
}
with an initializer like this in your window controller subclass:
- (id) initWithPerson: (Person *) aPerson {
self = [super initWithWindowNibName:@"PersonWindow"]; // owner
defaults to "self"
if (!self)
return;
person = aPerson; // set the ivar -- also retain it, if you're not
using garbage collection
[self showWindow: self];
return self;
}
The window never opens and I see the message:
Cannot create BOOL from object <NSTextField: 0x16bba0> of class
NSTextField
I don't know if this is because I didn't set up the bindings
correctly or if something else is wrong (I suspect both).
Most likely it's because you specified the wrong object as "owner"
when initializing the window controller.
Also, should my Person class just have a NSMutableDictionary (for
storing the first and last names) or should I declare NSString ivars
for the first and last names in the person class (as properties so
they have the settters/getters necessary to work with bindings).
Ivars would be the better way to go.
_______________________________________________
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