2014-09-25 60 views
0

因此,下面的代码会生成一个异常,因为alertView在我试图展示它时为零。为什么这个alertView无?

是否有一些原因,这是一个不好的分配/初始组合?这不能在类方法或其他方法中完成吗?

谢谢。

+(void) Test 
{ 
    UIAlertView * alertView = [[UIAlertView alloc] 
           initWithTitle:@"t" 
           message:@"message" 
           delegate:nil 
           cancelButtonTitle:@"" 
           otherButtonTitles:nil]; 

    [alertView show]; 

} 
+0

尝试宣告它在强或保留。 – Bejibun 2014-09-25 18:02:31

+0

我尝试在iOS 8中,它正在工作。 – caglar 2014-09-25 18:19:33

+2

什么是例外?您发布的代码应该正常工作,没有错误。 – rmaddy 2014-09-25 18:32:12

回答

0

在iOS 8之前,您必须有一个非空字符串cancelButtonTitle。变化:

cancelButtonTitle:@"" 

要:

cancelButtonTitle:@"ok" 
+1

在iOS 8中,即使将cancelButtonTitle设置为零,也会显示警报视图。当然,设置nil是不正确的。 – caglar 2014-09-25 18:22:49

+0

@caglar有趣的感谢。我会更新我的答案。 – ChrisH 2014-09-25 18:24:22