伙计们:
在我的测试应用程序的viewController中有两个按钮,右边的那个我称之为"NO",
另一个是“是”。这两个按钮将调用两个不同的函数,
用户按下其中一个按钮,我想向用户显示一个警报以确认这一点。
我知道使用UIAlertViewDelegate
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex但是有两个按钮,我很困惑。我怎么知道哪个按钮被按下了。
所以,请帮我解决这个问题,提前谢谢!
发布于 2012-04-02 20:44:36
创建UIAlertView时,可以为其设置tag
-(IBAction)yesButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil];
alert.tag = 101;
[alert show];
}
-(IBAction)noButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil];
alert.tag = 102;
[alert show];
}在delegate方法中,检查正在显示哪个警报
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 101) {
// from YES button
}
else if (alertView.tag == 102) {
// from NO button
}
}发布于 2012-04-02 20:34:11
- (void)alertView:(UIAlertView *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex{
switch(buttonIndex){
case 0:
//YES button handler
break;
case 1:
//NO button handler
break;
default:
break;
}
}发布于 2012-04-02 20:43:14
您可以使用tag属性在两个UIAlertView之间进行区分
在按钮1的功能中
alertView1.tag=1;
和在
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag==1){
//first button was clicked
}
}https://stackoverflow.com/questions/9976471
复制相似问题