2011-09-20 112 views
3

我想知道下面的代码是否可以。我想从“timedAlert”方法中2秒后自动关闭alertView(并且没有alertView中的任何按钮)。UIAlertView没有任何按钮

//this is in another method 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [self timedAlert]; 
} 

-(void)timedAlert 
{ 
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:2]; 
} 

-(void)dismissAlert:(UIAlertView *) alertView 
{ 
    [alertView dismissWithClickedButtonIndex:nil animated:YES]; 
} 

如果alertView的cancelButton被设置为 “无”,将如何在 “[alertView dismissWithClickedButtonIndex:0动画:是];”事情工作?我试图使cancelButton“无”和它的工作,但不能弄清楚如何....

P.S:我叫timedAlert方法从另一个

任何帮助表示赞赏!谢谢!

+2

是的,你的代码是好的。你可以在这里阅读更详细的解释:http://iphonedevelopertips.com/user-interface/uialertview-without-buttons-please-wait-dialog.html – adamsiton

+0

谢谢!但我仍然得到disviewWithClickedBittonIndex如何在alertView中没有按钮时正常工作。特别是为什么按钮索引被接受...请帮助! – Balaram

+0

我不太明白你的问题。在dismissWithClickedBittonIndex方法中,按钮索引是可选的。如果你通过零(或0),比警报简单地解雇。 – adamsiton

回答

1

你的代码应该可以工作,而且你应该没有问题。我已经在我之前的一个应用程序中完成了这一点。该按钮不显示,因为标题为零,但我认为该按钮的实例仍然存在。在关闭警报之前放置一个断点并查看警报变量,然后检查是否有按钮数组或其他东西,这应该告诉你这是如何工作的。

+0

有一个按钮可变数组,但它显示“0对象”... – Balaram

+0

theres也NSInteger值读取-1取消按钮,默认按钮,第一个其他按钮..这就是对的?那是因为dismissWithClickedButtonIndex接受索引的零值????? – Balaram

+0

我还看到一个“_dismissButtonIndex”字段,它读取0值!我希望这一切都说!我对么? – Balaram

4

首先我要说,如果你处理这与自定义视图可能会更好,但认为问题看起来是与

[alert release]; 

您正在释放的对象,你用它做前(我很惊讶它不会崩溃)。

做这样的事情

// other code 
alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
    [alert show]; 
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:3.0f]; 
} 

-(void)dismissAlert:(UIAlertView *) alertView 
{ 
    [alertView dismissWithClickedButtonIndex:nil animated:YES]; 
    [alertView release]; 
} 
+0

感谢您的建议!这看起来更可以接受! – Balaram

+0

它不会崩溃,因为[alert show]保留了对象,'alert'变量仍然指向它。这个答案中的修改代码是可以的,但[alert release]可以留在[self performSelector ....]之后的代码中,并从dismissAlert视图中移除。或者现在转移到ARC以摆脱所有这些混乱。 –