xcode IOS:具有两种不同委托方法的两个 UIAlert

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6276819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:13:01  来源:igfitidea点击:

IOS: two UIAlert with two different delegate methods

objective-cxcodeiosuialertview

提问by cyclingIsBetter

I have an UIAlert

我有一个 UIAlert

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ok" 
                                                        message:@"Canc?"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:@"Annul", nil];
[alertView show];
[alertView release];

and its delegate method:

及其委托方法:

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

if(buttonIndex == 0)//OK button pressed
{
    //do something
}
else if(buttonIndex == 1)//Annul button pressed.
{
    //do something
}

and it's all ok but if I have another alertview example "alertViewOne", I want that this alertViewOne have its delegate method and it shouldn't use delegate method of first alertview; how does change my code?

一切都可以,但是如果我有另一个警报视图示例“alertViewOne”,我希望这个 alertViewOne 有它的委托方法,它不应该使用第一个警报视图的委托方法;如何更改我的代码?

回答by shannoga

Simply set a tag to each Alert view and check which one sent the messeg.

只需为每个警报视图设置一个标签,然后检查哪个发送了消息。

alertView.tag=0;

And then

进而

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex              {
  if(alertView.tag==0){

     if(buttonIndex == 0)//OK button pressed
    {
? ? //do something
    }
    else if(buttonIndex == 1)//Annul button pressed.
    {
? ? //do something
    }
}else{
    if(buttonIndex == 0)//OK button pressed
    {
? ? //do something
    }
      else if(buttonIndex == 1)//Annul button pressed.
    {
? ? //do something
    }
  }

UpdateThere is a better solution using blocks.

更新使用块有更好的解决方案。

You can look at this project for example: UIAlertView-Blocks

你可以看看这个项目,例如: UIAlertView-Blocks

And as far as I know iOS8 will come with native alerts with blocks.

据我所知,iOS8 将带有带有块的本机警报。