首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertViewDelegate:clickedButtonAtIndex和两个按钮

UIAlertViewDelegate:clickedButtonAtIndex和两个按钮
EN

Stack Overflow用户
提问于 2012-04-02 20:30:31
回答 3查看 6.9K关注 0票数 6

伙计们:

在我的测试应用程序的viewController中有两个按钮,右边的那个我称之为"NO",

另一个是“是”。这两个按钮将调用两个不同的函数,

用户按下其中一个按钮,我想向用户显示一个警报以确认这一点。

我知道使用UIAlertViewDelegate

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

但是有两个按钮,我很困惑。我怎么知道哪个按钮被按下了。

所以,请帮我解决这个问题,提前谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-02 20:44:36

创建UIAlertView时,可以为其设置tag

代码语言:javascript
复制
-(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方法中,检查正在显示哪个警报

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 101) {
        // from YES button
    }
    else if (alertView.tag == 102) {
        // from NO button
    }
}
票数 17
EN

Stack Overflow用户

发布于 2012-04-02 20:34:11

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)actionSheet
    clickedButtonAtIndex:(NSInteger)buttonIndex{
  switch(buttonIndex){
    case 0:
      //YES button handler
      break;
    case 1:
      //NO button handler
      break;
    default:
      break;
   }
}
票数 0
EN

Stack Overflow用户

发布于 2012-04-02 20:43:14

您可以使用tag属性在两个UIAlertView之间进行区分

在按钮1的功能中

alertView1.tag=1;

和在

代码语言:javascript
复制
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag==1){
//first button was clicked
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9976471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档