UIAlertViewDelegate:clickedButtonAtIndex和两个按钮

时间:2012-04-02 12:30:31

标签: ios uialertview

人:

我的viewController中有两个按钮,我称之为“NO”,

另一个是“是”。这两个按钮将调用两个不同的功能,当

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

我知道使用UIAlertViewDelegate

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

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

所以,请帮助我,提前谢谢你!

3 个答案:

答案 0 :(得分:17)

创建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];
}

在委托方法中检查正在显示哪个警报

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 101) {
        // from YES button
    }
    else if (alertView.tag == 102) {
        // from NO button
    }
}

答案 1 :(得分:0)

- (void)alertView:(UIAlertView *)actionSheet
    clickedButtonAtIndex:(NSInteger)buttonIndex{
  switch(buttonIndex){
    case 0:
      //YES button handler
      break;
    case 1:
      //NO button handler
      break;
    default:
      break;
   }
}

答案 2 :(得分:0)

您可以使用tag属性来区分您的UIAlertView 在按钮1的功能中 alertView1.tag=1;
并在

-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag==1){
//first button was clicked
}

}
相关问题