Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController showing with delay

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

like image 301
user3197643 Avatar asked Oct 19 '14 10:10

user3197643


People also ask

What is the use of uialertcontroller?

UIAlertController is one of the most basic component of the app development, because using the UIAlertController to get the feedback, confirmation, choose between the options from the user, also we do alerts. Title : The title of the alert. Alert Message : Descriptive text that provides more details about the reason for the alert.

Why does the alert controller have a reference to each field?

The alert controller maintains a reference to each text field so that you can access its value later. The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

How do I add text fields to the alert interface?

When configuring an alert with the UIAlertController.Style.alert style, you can also add text fields to the alert interface. The alert controller lets you provide a block for configuring your text fields prior to display. The alert controller maintains a reference to each text field so that you can access its value later.

How do I configure alerts and action sheets in UIKit?

Use this class to configure alerts and action sheets with the message that you want to display and the actions from which to choose. After configuring the alert controller with the actions and style you want, present it using the present (_:animated:completion:) method. UIKit displays alerts and action sheets modally over your app's content.


2 Answers

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

like image 182
Tomusm Avatar answered Sep 23 '22 07:09

Tomusm


    DispatchQueue.main.async {         self.present(alertView, animated: true, completion:nil)     } 

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

like image 25
Mikrasya Avatar answered Sep 24 '22 07:09

Mikrasya