在UIAlterView的代理方法中 点击确定时 添加一个UIView到[[UIApplication sharedApplication].keyWindow上只出现了0.5秒左右就消失的问题


昨天,遇到一个很奇葩的问题,看下面代码:

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            [self.navigationController popViewControllerAnimated:YES];
            break;
        }
        case 1:
        {
            UIView *v = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            v.backgroundColor = [UIColor redColor];
            [[UIApplication sharedApplication].keyWindow addSubview:v];
            break;
        }
        default:
            break;
    }
}

在弹出一个警告框的时候,我想点击确定按钮,然后加一个viewwindow上,可是我发现,这个view只出现了零点几秒就消失了。

在我无助的时候,请教了老大,发现原来alertview也是加在window上的,在点击确定的时候,删除alterview的同时,把刚添加的view也跟着删除了。

所以,我就延时一段时间后在生成这个view:

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            [self.navigationController popViewControllerAnimated:YES];
            break;
        }
        case 1:
        {
            
            [self performSelector:@selector(delayView) withObject:nil afterDelay:0.6];
            break;
        }
        default:
            break;
    }
}

- (void)delayView{
    UIView *v = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    v.backgroundColor = [UIColor redColor];
    [[UIApplication sharedApplication].keyWindow addSubview:v];
}


这样view就不会出现消失的情况了,注意延时的时间要大于0.5秒。




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值