天天看點

在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;
    }
}
           

在彈出一個警告框的時候,我想點選确定按鈕,然後加一個view到window上,可是我發現,這個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秒。

繼續閱讀