今天在开发过程中遇到一个问题,在使用UIAlertController的时候出现警告,且不弹出警告框,警告信息如下:
总所周知,在iOS8之前我们都是使用UIAlertView的,所以不会出现这问题。但今年iOS9发布后,UIAlertView就被弃用了,用UIAlertController封装了UIAlertView和UIActionSheet这两个控件的;
然而我在开发中我在使用UIAlertController的页面是通过上一个页面presentViewController过来的,而使用UIAlertController也需要在当前页面也需要presentViewController,如下:
[self presentViewController:alert animated:YES completion:nil];找不到就出现警告,且不出现弹出框;
通过网上查阅资料,通过GCD在开启一个线程的得到了解决,当然关于GCD我也了解不是很 透彻,在里面我们开发送指定线程去执行;
使用了dispatch_async是将block发送到指定线程去执行,当前线程不会等待,会继续向下执行,在另外一个线程中处理这些操作,然后通知主线程更新界面。
核心代码如下,
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"xxxxxxxxxx" preferredStyle:UIAlertControllerStyleAlert];
__weak PhotoViewControl *weakSelf = self;
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancelButton];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// //在另外一个线程中处理这些操作,然后通知主线程更新界面。
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
});
加上 dispatch_async就能解决之前的警告,弹出对应的弹出框;