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

来源:互联网 发布:开淘宝店怎么找客户 编辑:程序博客网 时间:2024/05/22 10:56

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  2. {  
  3.     switch (buttonIndex) {  
  4.         case 0:  
  5.         {  
  6.             [self.navigationController popViewControllerAnimated:YES];  
  7.             break;  
  8.         }  
  9.         case 1:  
  10.         {  
  11.             UIView *v = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];  
  12.             v.backgroundColor = [UIColor redColor];  
  13.             [[UIApplication sharedApplication].keyWindow addSubview:v];  
  14.             break;  
  15.         }  
  16.         default:  
  17.             break;  
  18.     }  

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

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

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  2. {  
  3.     switch (buttonIndex) {  
  4.         case 0:  
  5.         {  
  6.             [self.navigationController popViewControllerAnimated:YES];  
  7.             break;  
  8.         }  
  9.         case 1:  
  10.         {  
  11.               
  12.             [self performSelector:@selector(delayView) withObject:nil afterDelay:0.6];  
  13.             break;  
  14.         }  
  15.         default:  
  16.             break;  
  17.     }  
  18. }  
  19.   
  20. - (void)delayView{  
  21.     UIView *v = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];  
  22.     v.backgroundColor = [UIColor redColor];  
  23.     [[UIApplication sharedApplication].keyWindow addSubview:v];  


这样view就不会出现消失的情况了,注意延时的时间要大于0.5秒。
1 0
原创粉丝点击