关于UIAlertController的whose view is not in the window hierarchy!问题解决

今天在开发过程中遇到一个问题,在使用UIAlertController的时候出现警告,且不弹出警告框,警告信息如下:


总所周知,在iOS8之前我们都是使用UIAlertView的,所以不会出现这问题。但今年iOS9发布后,UIAlertView就被弃用了,用UIAlertController封装了UIAlertViewUIActionSheet这两个控件的;

然而我在开发中我在使用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就能解决之前的警告,弹出对应的弹出框;


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值