Re: NSRunAlertPanel-not working?
Re: NSRunAlertPanel-not working?
- Subject: Re: NSRunAlertPanel-not working?
- From: Ryan Bates <email@hidden>
- Date: Tue, 17 Feb 2004 08:54:40 -0800
On Feb 16, 2004, at 8:21 PM, James McConnell wrote:
Thanks for the tip! I've played around with bindings in Xcode, and I
honestly just can't seem to get a grip on them, at least not how they
apply
to my project. I have a doc-based app that has basically one window
with 7
text fields. Each text field represents a value in a model class
(extends
NSObject). As a text field is filled and focus moves, the value of
the text
field is copied to the model class. When the user saves, I simply
archive
the object, then unarchive when they open it.
Honestly, I haven't seen enough documentation/examples to fully
understand
how bindings work and how to use them in my application. I don't know
if
I'm stupid and just can't get it, but it just isn't sinking in. So
until I
know exactly how they work and how to use them, I'm doing things the
"old"
way.
The concept of bindings was hard for me to grasp as well. Apple's
documentation was a little confusing, but while I was doing the three
examples at the end, everything just "clicked." I suggest you skim
through the first couple articles in Cocoa Bindings then jump right in
to the examples. After that, go back and read all the documentation. It
is quite amazing what you can accomplish with so little code.
Unfortunately I haven't had the opportunity to use it in anything
productive because I want to support 10.2. Here's the docs:
http://developer.apple.com/documentation/Cocoa/Conceptual/
CocoaBindings/index.html
Now, to get back on topic (kinda):
I suggest you make the document the delegate of the text fields. You do
this by control-clicking the text field then drag on to the File's
Owner (which is the instance of MyDocument) in Interface Builder. Then
choose "delegate" in the info panel and hit connect. In the MyDocument
class, implement this method:
// The NSTextField calls this method right before it ends editing
// If it returns NO then NSTextField doesn't allow the focus to leave.
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText
*)fieldEditor
{
if ([[control stringValue] isEqualToString:@""])
{
NSRunAlertPanel(@"Empty field", @"Please enter a value.",
@"OK", nil, nil);
return NO;
}
return YES;
}
You can then remove the notifications unless you need it for something
else. You can dump the validation method as well.
NSFormatter is probably the right way to do this, especially if you
want a more complicated validation check (for example, to see if a
phone number or zip code is correct). It involves subclassing
NSFormatter and applying it to all of the text fields. Most of this can
be done through IB. However, I suggest sticking with the above example
for now because it's simpler IMHO.
Ryan
_______________________________________________
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.