当有多个警告框时如何检测按钮按下的事件

27

我在一个视图中有多个警报视图,我使用以下代码来检测哪个按钮被按下:

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

    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];  

    if ([title isEqualToString:@"OK"]) {

          //for one alert view
          [passCode becomeFirstResponder];

     } else if ([title isEqualToString:@" OK "]) {

        //for another alert view, had to change "OK" to " OK "
        [passCodeConfirm becomeFirstResponder];

    }
}   

现在,由于一个视图中有多个警告框执行不同的操作,我必须欺骗用户认为"OK"和" OK "是相同的东西。这样做虽然可行且看起来不错,但感觉有些混乱。肯定有其他方法可以解决这个问题,例如将其特定于一个警告框,然后将其特定于另一个警告框。你知道我该如何做吗?谢谢!

5个回答

57

为了技术上更好,也更好管理,最好为单独的UIAlertView设置唯一的tag,并在其委托方法中进行识别和访问。

例如:

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Are You Sure you want to Update?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
    [alert setTag:1];
    [alert show];
    [alert release];

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
    {
        if(alertView.tag == 1)
        {
            // set your logic
        }
    }

我喜欢这个比我之前的解决方案更好。 - gamozzii
非常感谢你的帮助!标签可以分配给几乎任何用户界面对象吗? - Jack Humphries
所有支持getter/setter属性的UI对象,开发人员都可以进行分配标签。提供了同一类别的类必须具有唯一标识符以正确获取响应。 - alloc_iNit
我建议不要在代码中使用神奇数字,这确实会使你的代码难以阅读,我发现自己不得不在自己的代码中搜索神奇数字,才能找出哪个对象与哪个数字相关。请遵循Gamozzii在下面帖子中提到的方法。 - Pavan
@Pavan - 我认为创建多个UIAlertView对象不是一个好的做法。我更喜欢使用唯一标识符。当然,可以定义有意义的宏在整个项目中使用。 - alloc_iNit
@alloc_iNit,我已经给出了偏好方法的逻辑理由。你能否用同样的方式解释一下你的方法,以便你能够证明你的论点呢?谢谢啦,伙计。 - Pavan

4

使用tag属性来唯一标识您创建的每个警报视图。

像这样:

myAlertView.tag = 1

然后在clickedButtonAtIndex委托方法中,使用标签属性检查哪个警报框的按钮被点击了,

if(alertView.tag==1)

2

根据您的意见,在每个弹出视图中添加一个属性。

UIAlertView *myAlertType1;
UIAlertView *myAlertType2;

@property (nonatomic, retain) UIAlertView *myAlertType1;
@property (nonatomic, retain) UIAlertView *myAlertType2;

使用以下属性创建您的警报:
self.myAlertType1 = [[[UIAlertView alloc] initWithTitle: ... etc] autorelease];
[self.myAlertType1 show];

然后在你的委托方法中:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView == myAlertType1) {
         // check the button types and add behaviour for this type of alert
    } else if (alertView == myAlertType2 {
         // check the button types and add behaviour for the second type of alert
    }
}

编辑:尽管以上方法可行,但iApple建议使用标签看起来更干净简单。


2
我不喜欢使用标签,因为当对象本身应该告诉你需要知道的信息时,你必须添加和访问额外的信息。 - Joshua Dance

2

我不会使用标题来区分按钮。当您的应用程序本地化或您决定更改按钮标题但忘记在所有位置更新它们时,您将遇到问题。相反,使用按钮索引,如果除了取消按钮之外只有一个按钮,则使用UIAlertViewcancelButtonIndex属性。

要区分多个警报视图,可以使用它们的tag属性。


1
 //in your .h file
  UIAlertView* alert1;
  UIAlertView* alert2;

  //in your .m file
  // when you are showing your alerts, use
  [alert1 show]; //or
  [alert2 show];

  //and just check your alertview in the below method

  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  {  
       if(alertView == alert1)
       {
              //check its buttons
       }
       else   //check other alert's btns
  }   

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接