Re: NSRunAlertPanel-not working?
Re: NSRunAlertPanel-not working?
- Subject: Re: NSRunAlertPanel-not working?
- From: Ryan Bates <email@hidden>
- Date: Mon, 16 Feb 2004 19:30:01 -0800
On Feb 16, 2004, at 6:50 PM, James McConnell wrote:
if (![name stringValue])
Here you are testing if the "stringValue" is equal to nil, but instead
you want to test if it is equal to a blank string (there is a
difference). Try this:
if ([[name stringValue] isEqualToString:@""])
Anyone care to point me in the right direction here? I have 7 text
fields
to do this with, so once I get the first one working, the rest is just
copy/paste.
Be careful with copy and paste! Duplicate code is a bad thing. Just
think, if you ever wanted to change the message in the alert dialog
then you would need to change it seven times. I suggest refactoring out
the validation check like this:
// Returns YES if field is valid
- (BOOL)validateTextField:(NSTextField *)textField
{
if ([[textField stringValue] isEqualToString:@""]) {
NSRunAlertPanel(@"Empty field", @"Please enter a value.",
@"OK", nil, nil);
return NO;
}
return YES;
}
You could then call this method for each field - even then you could
probably refactor a bit more. Also, you may want to look into using
Interface Builder actions instead of listening for the notifications.
Hope that helps.
Ryan
On Feb 16, 2004, at 6:50 PM, James McConnell wrote:
Greetings, all. I have a small personal app I'm working on, and I'm
trying
to implement some user validation. I have a doc-based app with some
text
fields. No big deal. I also have an NSNotification for each field to
set a
value in a model object when focus is lost (using
NSControlTextDidEndEditingNotification). This is working fine. What I
would like to do is check if the field is blank after it's lost focus.
If
it is, I want an alert panel telling the user they must enter a value.
I
only need it to have one "OK" button, and then return focus to the text
field. I think I have that setup, but my code is not working. Here
is the
troublesome snippet. I'm sure I have something wrong here, but I just
can't
tell what. Below is the notification method. Again, setting the
value in
the model object works, but I can't get the alert panel to show:
*** START CODE ***
- (void)updateName:(NSNotification*)notification
{
if (![name stringValue])
{
int choice = NSRunAlertPanel(@"Empty field", @"Please enter a
value.", nil, nil, nil);
switch(choice)
{
case NSAlertDefaultReturn:
break;
case NSAlertAlternateReturn:
break;
case NSAlertOtherReturn:
break;
}
}
[recipe setName:[name stringValue]]; // If I chop out the rest of
the
code, and simply use this, it works fine. It sets the value in the
model
object to what is typed, and to null if nothing.
}
*** END CODE ***
Anyone care to point me in the right direction here? I have 7 text
fields
to do this with, so once I get the first one working, the rest is just
copy/paste. Thanks again. BTW, I'm on OS X 10.3, but using Project
Builder
as this project was started in PB and I'm not used to Xcode enough to
switch
my environment on my project just yet. I know, I know, PB isn't
supported,
just humor me. ;-)
James
_______________________________________________
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.