scsiduck
scsiduck

Reputation: 43

UIAlertView not dismissing?

I have a few ViewController subclasses inside a UINavigation controller. I have successfully used UIAlertViews elsewhere in the application, and I know how to set the delegate and include the correct delegate methods, etc.

In a ViewController with a UITableView, I have implemented a 'pull to refresh' with a UIRefreshControl. I have a separate class to manage the downloading and parsing of some XML data, and in the event of a connection error, I post a notification. The view controller containing the table view observes this notification and runs a method where I build and display an alert:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:[[notification userInfo] objectForKey:@"error"] delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
alertView.alertViewStyle = UIAlertViewStyleDefault;
[alertView show]; 

The alert displays correctly, but the cancelButton is unresponsive - there is no way to dismiss the alert! Putting similar code (identical, but without the notification's userinfo) in the VC's viewDidLoad method creates an alert that behaves normally.

Is the refresh gesture hogging first responder or something? I have tried [alertView becomeFirstResponder]. I would be grateful for any advice…

Update: screenshot included… is this the right info? (can't embed this image for lack of reputation) https://i.sstatic.net/4CGqS.png

Upvotes: 1

Views: 2506

Answers (3)

Léo Natan
Léo Natan

Reputation: 57040

Edit

It seems like you have a deadlock or your thread is stuck waiting. You should look at your code and see what causes this.

Original answer which lead to update in OP

Make sure the alert is shown on the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    //Open alert here
});

Upvotes: 3

timgcarlson
timgcarlson

Reputation: 3146

Try adding a tag to the alertView

alertView.tag = 0;

Then create the method alertView:clickedButtonAtIndex: in the view controller.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if (alertView.tag == 0) {
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
    }
}

Upvotes: 0

Fluffhead
Fluffhead

Reputation: 845

This isn't a solution per se, but I might try two quick things as you troubleshoot:

1) Hardcode some text in the UIAlert, rather than passing in the notification object. See if there is any change in behavior.

2) Try adding another button to the alert and an accompanying method to catch it. So you'll see if the delegate is getting ny buttons messages at all.

Upvotes: 0

Related Questions