Iphone 存在多个警报视图时按下的检测按钮

Iphone 存在多个警报视图时按下的检测按钮,iphone,ios,ipad,uialertview,Iphone,Ios,Ipad,Uialertview,我在一个视图中有多个警报视图,我使用此代码检测按下了哪个按钮: -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; if ([title isEqualToString:@"OK"]) { //for

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

-(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”是同一件事。它工作正常,看起来不错,但感觉有点凌乱。当然还有另一种方法可以做到这一点,例如将其特定于警报视图,然后将其特定于另一个视图。你知道我会怎么做吗?谢谢

我不会用标题来区分按钮。当你的应用程序被本地化,或者你决定更改按钮标题,但忘记在任何地方更新时,你都会遇到问题。使用按钮索引,或者如果除了取消按钮之外只有一个按钮,请使用
UIAlertView
cancelButtonIndex
属性


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

最好为单独的UIAlertView设置唯一的标记,并在其委托方法中识别和访问它

比如说,

    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
        }
    }

在视图中,为每个警报视图添加属性

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建议使用标记似乎更简洁。

使用标记属性来唯一标识您创建的每个alertview

 //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
  }   
像这样

myAlertView.tag = 1
然后在ClickedButtonIndex委托方法中,检查使用此标记属性单击了哪个alertview的按钮

if(alertView.tag==1)

我比我的解决方案更喜欢这个,太好了,谢谢你的帮助!标签几乎可以分配给任何UI对象吗?开发者可以分配所有支持标签getter/setter属性的UI对象。但是,对于同一类,必须有一个唯一的标记才能正确地得到响应。我建议不要在代码中使用幻数,这确实会使代码很难阅读,我发现自己必须在自己的代码中搜索幻数,以便找出哪个对象与哪个数相关。遵循Gamozzii在下面帖子中的方法。@Pavan-我认为创建多个UIAlertView对象不是一个好的做法。我更喜欢用一个独特的标签。当然,可以定义有意义的宏来在整个项目中使用。我不喜欢使用标记,因为当对象应该告诉您需要知道什么时,您必须添加和访问额外的信息。