Corrado
Corrado

Reputation: 515

UIAlertView delay or not showing

I have used UIAlertView several times without problems but this time I can't make it work correctly. The (simple) code is the following:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    alert = [[UIAlertView alloc]initWithTitle:@""
                                      message:[NSString stringWithFormat: @"Reload data ?"]
                                     delegate:self
                            cancelButtonTitle:@"Yes"
                            otherButtonTitles:@"Cancel", nil];    
    [alert show];
    NSLog(@"executed");
}

If I touch in a row of the TableView I have two different behaviours:

  1. the alertView is shown after some (6/7) seconds
  2. the alertView is not shown. In this case, as soon as I touch in any point of the screen the alert is shown immediately.

In both cases, the [alert show] is executed immediately after the first touch because I see "executed" in the Log screen. Before and after the alert, the application is not doing anything else.

Any help ?

Upvotes: 0

Views: 2266

Answers (3)

Eric Wiener
Eric Wiener

Reputation: 5967

I also had this issue and for me the problem was I had two queues running simultaneously. I had UI changes occurring in the main queue (all UI changes are required to occur in the main queue) and I had a very computationally intensive task occurring in a separate queue. My initial set up was:

let alert = UIAlertController(title: nil, message: "Custom message", preferredStyle: .alert)
self.present(alert, animated: false, completion: nil)

// perform time intensive task in seperate queue

To fix the issue, I moved the intensive task into the completion callback:

let alert = UIAlertController(title: nil, message: "Custom message", preferredStyle: .alert)
self.present(alert, animated: false, completion: {
    // perform intensive task here
})

Upvotes: 0

Julian Minde
Julian Minde

Reputation: 166

Believe it or not but UIAlertView is in fact deprecated in iOS8.

That being said, you can still use it. For future project targeting iOS8+ Apple recommends that you use UIAlertViewController instead.

This is an example of using UIAlertViewController to open the application's settings:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Go to settings" message:@"Do you wish to go to the application settings?" preferredStyle:UIAlertControllerStyleAlert]; ;
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:@"Settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
[alertController addAction:cancelAction];
[alertController addAction:settingsAction];

[self presentViewController:alertController animated:YES completion:nil];

Upvotes: 0

Nekak Kinich
Nekak Kinich

Reputation: 934

Try with this

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
        alert = [[UIAlertView alloc]initWithTitle:@""
                                  message:[NSString stringWithFormat: @"Reload data ?"]
                                 delegate:self
                        cancelButtonTitle:@"Yes"
                        otherButtonTitles:@"Cancel", nil];    
       [alert show];
       NSLog(@"executed");
    }];

Upvotes: 2

Related Questions