tags:

views:

49

answers:

1

I am trying to run an action when the application starts. The action checkAtStart is supposed to display an alert if there is no text in field1 and hide startView if there is text in field1. checkAtStart works fine if assigned to a button, but when I try to run it using (void)awakeFromNib, the alert will display no matter what and startView will never hide. Its probably something really simple that I'm forgetting. Any help is appreciated!

Here is my code:

- (void)awakeFromNib {
 [self checkAtStart:self];
}

- (IBAction)checkAtStart:(id)sender
{
 if (field1.text == nil || [field1.text isEqualToString:@""]) {
     NSString *msg = nil;

     msg = nil;

     UIAlertView *alert = [[UIAlertView alloc] 
        initWithTitle:@"Test Message" 
        message:@"Test Message"
        delegate:self 
        cancelButtonTitle:@"Close" 
        otherButtonTitles: nil];
     [alert show];
     [alert release];
     [msg release];
 }
 else
 {
     startView.hidden = YES;
 }
}
+2  A: 

I'm assuming that all of this code is in a view controller of some sort.

-awakeFromNib will be sent to the controller after it's been loaded from a NIB. At this point there's no value in the text field yet unless you put those values in the NIB file.

You probably used -viewDidLoad or other view event messages to assign some value from your model into the text field. That's what's making your IBAction work when assigned to a button.

Giao
You are correct about the -viewDidLoad. Since the -viewDidLoad is assigning the value, I included [self checkAtStart:self]; at the end, and that seemed to do the trick. Thanks for the clarification!
MN