Reputation: 5936
I want an alert when the following text fields receive the conditions in second section of code
self.circuit.rcdAtIan = [ICUtils nonNilString:self.rcdAtIan.text];
self.circuit.rcdAt5an = [ICUtils nonNilString:self.rcdAt5an.text];
The above code works fine so now I need to fire it. Using the below method was my first thought but that fires the alert on every keyboard resignation. I only want the alert to display once.
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([_rcdAtIan.text intValue]> 200) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"my message" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[alert show];
}
if ([_rcdAt5an.text intValue]> 40) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"my message" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[alert show];
}
}
Im thinking maybe I need a bool with NSUserDefaults
perhaps? but not sure how to implement that to check if the alert has been shown. Normally if I wanted an alert shown once I would do
if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"alert"]]){
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert"];
[[NSUserDefaults standardUserDefaults] synchronize];
[alert show];
}
However in this instance, when the page is reloaded I want the alerts to be shown again and in any case im not sure if thats an efficient way to solve this
Upvotes: 0
Views: 138
Reputation: 31745
Don't use NSUserDefaults, that would be inappropriate
in your @interface...
@property (assign) BOOL freshAlert1;
@property (assign) BOOL freshAlert2;
then something like this... (assuming 'page is reloaded' equates to your viewController coming back onscreen)
- (void)viewDidAppear
{
self.freshAlert1 = YES;
self.freshAlert2 = YES;
}
if (([_rcdAtIan.text intValue]> 200) && self.freshAlert1) {
self.freshAlert1 = NO;
...
}
if (([_rcdAt5an.text intValue]> 40) && self.freshAlert2) {
self.freshAlert2 = NO;
...
}
Upvotes: 1
Reputation: 17882
What exactly do you mean by "once"? Once per app run, once per editing step?
Or maybe simply add two BOOL flags that you can reset whenever you want and simply to
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([_rcdAtIan.text intValue]> 200 && !alert1shown) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"my message" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
alert1shown = YES;
[alert show];
}
if ([_rcdAt5an.text intValue]> 40 && !alert2shown) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"my message" delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];
alert2shown = YES;
[alert show];
}
}
Upvotes: 1