43

I'm experiencing a problem with UIAlertController on my app now migrated to iOS8 with Date Picker inside.

Below is the code.

UIAlertController *AlertView = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];

 UIAlertAction *ok = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[AlertView dismissViewControllerAnimated:YES completion:nil];
}];

 UIAlertAction *set = [UIAlertAction actionWithTitle:NSLocalizedString(@"Set to today", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[self set_to_today:nil];
[AlertView dismissViewControllerAnimated:YES completion:nil];
[self.tableView reloadData];
}];

 UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[AlertView dismissViewControllerAnimated:YES completion:nil];
}];


 UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
 datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setDate:data_appo];
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

[AlertView.view addSubview:datePicker];
[AlertView addAction:ok];
[AlertView addAction:set];
[AlertView addAction:cancel];
[self.view bringSubviewToFront:datePicker];
[self presentViewController:AlertView animated:YES completion:nil];

UIAlertController and Date Picker is shown when the user select a row from UITableViewController.

The problem is the following: first time the users select the row everything works fine...but if the user select "Cancel" and then select de tate again the UIAlertController takes 2-3 seconds to show up...this happens also in the simulator...

I'm getting crazy....this makes my app have a bad user experience.

Any help will be strongly appreciated Thanks

Alex

user3197643
  • 451
  • 4
  • 4
  • Try to replace `self` with `AlertView` in the line with `bringSubviewToFront` and call `dismissViewControllerAnimated:completion:` on `self`. – Felix Oct 19 '14 at 13:24
  • Replacing self with AlertView mouthing changes. – user3197643 Oct 19 '14 at 14:35
  • If I call "dismissViewControllerAnimated:completion: on self" i get an error...not sure if i'm coding correctly...can you post a sample code? – user3197643 Oct 19 '14 at 14:36

2 Answers2

92

I was having the same issue with a UIAlertController presented by selecting a row from a UITableView. The first time everything worked fine, and then when the user triggered the alert again there was a few seconds delay before the alert was actually presented.

As a workaround I used GCD:

    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:AlertView animated:YES completion:nil];
    });

It is probably a bug since -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath is already executed on the main thread.

I submitted a bug report to Apple: rdar://19285091

Tomusm
  • 2,208
  • 19
  • 20
27
    DispatchQueue.main.async {
        self.present(alertView, animated: true, completion:nil)
    }

Swift 3.0 version. Alternatively, setting animated: false also solved my problem.

Mikrasya
  • 964
  • 12
  • 21