I am calling a UIAlertView within it's own delegate and it is failing. The code is simple:
@interface ViewController () <UIAlertViewDelegate>
@property (nonatomic, strong) UIAlertView *alertView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.alertView = [[UIAlertView alloc] initWithTitle:@"Howdy"
message:@"Here's the alert"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[self.alertView show]; // this shows the
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
[self.alertView show]; // this does not show the alert again!
}
}
@end
However, when I remove:
[self.alertView show]
and replace it with:
[self.alertView performSelector:@selector(show) withObject:nil afterDelay:0.01]
it works.
This seems to indicate that the original UIAlertVIew
is not completely dismissed even though I am inside the delegate method alertView:didDismissWithButtonIndex:
.
While this is working, it does not seem right. Can anyone tell me what I am doing wrong?
You are probably right, but I don't see why would you show the same alert again. Since you usually do not require to keep a reference to an alert, you could just write a method like:
- (void)showAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Howdy"
message:@"Here's the alert"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[alert show];
}
It would also be better if you would call this from viewDidAppear
instead of viewDidLoad
.