2009-12-23 197 views
3

我的AlertView与“cancelButtonTitle:@”取消“”和“otherButtonTitles:nil”完美配合。我的问题是如何获得其他按钮。如何创建“otherButtonTitles”?

当我只更改“otherButtonTitles:@”2nd Button“”,那么iPhone模拟器刚刚崩溃了应用程序并进入主屏幕。

回答

10

要结束你的方法调用是这样的:

... cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1Title", @"Button2Title", nil];

这是你看到同样的模式在字符串格式化,其中的参数列表可以是任意长度的。通常参数列表然后以零结尾。不要忘记零。

+0

不知道关于零。完美的作品,谢谢! – Devoted 2009-12-23 23:55:36

6

确切like Kevin said,但作为附录,您还可以将目标操作分配给其他按钮。

当你实例化UIAlertView中,delegate参数设置为self,然后添加下面的方法到对象:

-(void) alertView: (UIAlertView *) alertView 
     clickedButtonAtIndex: (NSInteger) buttonIndex { 
     // do stuff 
     // if you want the alert to close, just call [ alertView release ] 
} 

`

+0

如何控制哪个按钮将激活此方法?我试了一下,无论按哪个按钮(包括取消),它都会运行这个方法。噢噢噢!我在这个方法中设置了一个条件来检查哪个按钮来做什么? – Devoted 2009-12-23 23:58:45

+0

是的。你说对了。 – 2009-12-24 00:12:09

+0

像'[self performSelector:[buttonActions objectAtIndex:buttonIndex]];' 这将工作,如果你有一个名为'buttonActions'的选择器 - 只是一个例子。 – 2009-12-24 00:13:59