xcode UIAlertController 显示延迟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26449724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:55:22  来源:igfitidea点击:

UIAlertController showing with delay

objective-cxcodeswiftuialertviewuialertcontroller

提问by user3197643

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

我的应用程序上的 UIAlertController 出现问题,现在已迁移到 iOS8,内部带有日期选择器。

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.

当用户从 UITableViewController 中选择一行时,会显示 UIAlertController 和日期选择器。

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...

问题如下:第一次用户选择行时一切正常......但是如果用户选择“取消”然后再次选择细节,UIAlertController 需要 2-3 秒才能显示......这也会发生在模拟器...

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

我快疯了……这让我的应用程序的用户体验很差。

Any help will be strongly appreciated Thanks

任何帮助将不胜感激谢谢

Alex

亚历克斯

回答by Tomusm

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.

我在通过从 UITableView 中选择一行来呈现 UIAlertController 时遇到了同样的问题。第一次一切正常,然后当用户再次触发警报时,在实际显示警报之前有几秒钟的延迟。

As a workaround I used GCD:

作为一种解决方法,我使用了 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 *)indexPathis already executed on the main thread.

这可能是一个错误,因为-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath它已经在主线程上执行了。

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

我向 Apple 提交了错误报告:rdar://19285091

回答by Mikrasya

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

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

斯威夫特 3.0 版本。或者,设置 animation: false 也解决了我的问题。