当有多个警报视图时检测按下的按钮

发布于 11-30 21:18 字数 655 浏览 1 评论 0原文

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

-(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];

    }
}   

现在,由于一个视图中有多个警报视图执行不同的操作,我必须欺骗用户认为“确定”和“确定” “是同一件事。它工作起来并且看起来不错,但感觉有点混乱。当然还有另一种方法可以做到这一点,例如使其特定于警报视图,然后使其特定于另一个警报视图。你知道我会怎么做吗?谢谢!

I have multiple alert views in one view, and I use this code to detect which button was pressed:

-(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];

    }
}   

Now since there are multiple alert views in one view that do different things, I have to trick the user into thinking "OK" and " OK " are the same thing. It works and looks fine, but it feels kind of messy. Surely there is another way to do this, such as making this specific to an alert view, and then making it specific to another. Do you know how I would do this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海里的那抹蓝2024-12-07 21:18:19

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

It would be more technical as well better that set unique tag for separate UIAlertView and identify it and access in its delegate method.

For example,

    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
        }
    }
机场等船2024-12-07 21:18:19

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

像这样

myAlertView.tag = 1

然后在 clickedButtonAtIndex 委托方法中检查使用此标记属性单击了哪个警报视图的按钮,

if(alertView.tag==1)

Use tag property to uniquely identify each of the alertview u create.

Like this

myAlertView.tag = 1

Then in the clickedButtonAtIndex delegate method check which alertview's button was clicked using this tag property ,

if(alertView.tag==1)
凡尘雨2024-12-07 21:18:19

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

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

I wouldn't use the titles to distinguish between the buttons. You'll run into problems when your app is localized or you decide to change the button titles, but forget to update them everywhere. Use the button indexes instead or if you only have one button in addition to a cancel button, use the cancelButtonIndex property of UIAlertView.

To distinguish between multiple alert views, you could use their tag property.

溺渁∝2024-12-07 21:18:19

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

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

In your view, add a property for each alert view.

UIAlertView *myAlertType1;
UIAlertView *myAlertType2;

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

Create your alert using these properties

self.myAlertType1 = [[[UIAlertView alloc] initWithTitle: ... etc] autorelease];
[self.myAlertType1 show];

Then in your delegate method:

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

Edit: Although the above works, iApple's suggestion of using the tag seems cleaner/simpler.

猫卆2024-12-07 21:18:19
 //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
  }   
 //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
  }   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文