【问题标题】:Detecting button pressed when there are multiple alert views当有多个警报视图时检测按钮被按下
【发布时间】:2011-08-19 06:58:17
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios ipad uialertview


    【解决方案1】:

    为单独的 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
            }
        }
    

    【讨论】:

    • 我比我上面的解决方案更喜欢这个
    • 太好了,感谢您的帮助!标签可以分配给几乎任何 UI 对象吗?
    • 所有支持标签getter/setter属性的UI对象,开发者可以分配。前提是,同一种类必须有一个唯一的标签才能正确获得响应。
    • 我建议不要在你的代码中使用幻数,它确实会让你的代码难以阅读,而且我发现自己不得不在自己的代码中搜索幻数才能找出哪个对象与哪个对象相关数字。在下面的帖子中遵循 Gamozzii 的方法。
    • @Pavan - 我认为创建多个 UIAlertView 对象不是一个好习惯。我更喜欢使用独特的标签。当然,可以定义有意义的宏以在整个项目中使用。
    【解决方案2】:

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

    这样

    myAlertView.tag = 1
    

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

    if(alertView.tag==1)
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        在您的视图中,为每个警报视图添加一个属性。

        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 使用标签的建议似乎更简洁/更简单。

        【讨论】:

        • 我不喜欢使用标签,因为当对象应该告诉你你需要知道什么时,你必须添加和访问额外的信息。
        【解决方案5】:
         //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
          }   
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-23
          • 2010-12-23
          相关资源
          最近更新 更多