xcode 如何确定哪个 UIAlertView 调用了委托。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7678341/
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 22:04:45  来源:igfitidea点击:

How to determine which UIAlertView called the delegate.

iphoneobjective-ciosxcode

提问by theNoobProgrammer

In the alertView delegate, theres a method:

在 alertView 委托中,有一个方法:

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

My question is, how can I find which AlertView called this delegate.

我的问题是,我怎样才能找到哪个 AlertView 调用了这个委托。

For example, I have a couple alert views which all use the delegate, but depending on which alertview called this method, I want to set up different actions for buttonIndex's.

例如,我有几个警报视图都使用委托,但是根据调用此方法的警报视图,我想为 buttonIndex 设置不同的操作。

回答by 5StringRyan

The "alertView" object that is being passed into the method is the actual alert that is being used in the method. The most straightforward way is to provide logic inside this method that looks at the alertView object (maybe looking at the name or tag? It's up to you), and then provides different actions for each.

传递给该方法的“alertView”对象是该方法中正在使用的实际警报。最直接的方法是在这个方法中提供逻辑来查看 alertView 对象(也许查看名称或标签?这取决于你),然后为每个对象提供不同的操作。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (alertView.tag == 1)
  {
    // do something
  }
  else if (alertView.tag == 2)
  {
    // do something else
  }

  // continue for each alertView

}

回答by Sam

In all of my projects I have a class called EasyAlertView. You can find similar code on the web, but the jist is you end up with an api like:

在我的所有项目中,我都有一个名为 EasyAlertView 的类。你可以在网上找到类似的代码,但问题是你最终得到了一个像这样的 api:

typedef void (^AlertBlock)(NSUInteger);

/** A less-unwieldly specialization of UIAlertView that allows a Block to be used as the dismissal handler. 
    This is more flexible and compact than the delegate based approach. It allows all the logic to
    be centralized within the launching method and eliminates confusion and object lifetime issues that arise
    when using multiple alerts in the same class bound to a single delegate. */
@interface EasyAlertView : UIAlertView <UIAlertViewDelegate>
{
    AlertBlock _block;
}

+ (id)showWithTitle:(NSString *)title 
            message:(NSString *)message 
         usingBlock:(void (^)(NSUInteger))block 
  cancelButtonTitle:(NSString *)cancelButtonTitle 
  otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

@end

This is nice because you can write code like:

这很好,因为您可以编写如下代码:

...
[EasyAlertView showWithTitle:@"Stuff happened"
                     message:@"All kinds of stuff just got flung around...  stop the flinging?"
                  usingBlock:^(NSUInteger buttonIndex) {
                     if (buttonIndex == 0) {  // user cancelled via "No" button
                     }
                     else if (buttonIndex == 1) { // user clicked "Yes"
                        // TODO: whatever cool logic you want here
                     }
                  }
           cancelButtonTitle:@"No"
           otherButtonTitles:@"Yes", nil];
...

This is nice since:

这很好,因为:

  1. You can handle the action where you also define the prompt
  2. Absolutely no question whatsoever as to what UIAlertView prompted this. (not checking against the UIAlertView title or tag, which are both typical approaches).
  1. 您可以处理您还定义提示的操作
  2. 毫无疑问,UIAlertView 提示了什么。(不检查 UIAlertView 标题或标签,这都是典型的方法)。

Please note that the action handler can call into another function to handle the behavior if you want, but the point is, it is very easy to associate behavior with the prompt. Before we used this code, we had some mazes of if / else statements in the UIAlertView protocol - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndeximplementation that checked against tags / titles of UIAlertViews. :( Was no bueno.

请注意,如果需要,动作处理程序可以调用另一个函数来处理行为,但重点是,将行为与提示关联起来非常容易。在我们使用这段代码之前,我们在 UIAlertView 协议- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex实现中有一些 if / else 语句的迷宫,它们检查 UIAlertViews 的标签/标题。:( 不是布埃诺。

Here is the EasyAlertView implementation:

这是 EasyAlertView 的实现:

#import "EasyAlertView.h"

@implementation EasyAlertView

- (void) dealloc
{
    [_block release], _block = nil;
    [super dealloc];
}

+ (id)showWithTitle:(NSString *)title message:(NSString *)message usingBlock:(void (^)(NSUInteger))block cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    EasyAlertView * alert = [[[EasyAlertView alloc] initWithTitle:title
                                                         message:message
                                                        delegate:nil
                                               cancelButtonTitle:cancelButtonTitle
                                               otherButtonTitles:nil] autorelease];

    alert.delegate = alert;
    alert->_block = [block copy];

    va_list args;
    va_start(args, otherButtonTitles);
    for (NSString *buttonTitle = otherButtonTitles; buttonTitle != nil; buttonTitle = va_arg(args, NSString*))
    {
        [alert addButtonWithTitle:buttonTitle];
    }
    va_end(args);

    [alert show];

    return alert;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (_block)
    {
        _block(buttonIndex);
    }
}

@end

Also see An enhancement for UIAlertView

另请参阅UIAlertView 的增强功能

回答by Bill Burgess

I realize this is kind of late, but came across this while looking for a solution to my own similar issue. I resolved this by assigning my different alerts as properties.

我意识到这有点晚了,但是在为我自己的类似问题寻找解决方案时遇到了这个问题。我通过将不同的警报分配为属性来解决这个问题。

This example assumes ARC and iOS 5, but should work as a good example. Hope this helps.

此示例假定 ARC 和 iOS 5,但应该作为一个很好的示例。希望这可以帮助。

@property (nonatomic, strong) UIAlertView *passwordAlert;
@property (nonatomic, strong) UIAlertView *warningAlert;

passwordAlert = [[UIAlertView alloc] initWithTitle:@"Password" message:@"Enter password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
passwordAlert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[passwordAlert show];

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
   if (alertView == passwordAlert) {
      // handle anything for password button indexes here
   }
   if (alertView == warningAlert)_ {
      // handle anything for warning button indexes here
   }
}

回答by beryllium

Use tagproperty

使用标签属性

回答by Mat

Set a tagto each alert you want to trigger, alterView.tag=10;for example, then in the body of the method you posted check the alert:

例如tagalterView.tag=10;为您要触发的每个警报设置一个,然后在您发布的方法的正文中检查警报:

if(alertView.tag==10){
   //your alert
}