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



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

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

我的问题是,我如何找到哪个AlertView调用了这个委托。

例如,我有几个警报视图,它们都使用委托,但取决于哪个alertview调用了这个方法,我想为buttonIndex设置不同的操作。

传递给方法的"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
}

在我所有的项目我有一个类叫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

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

...
[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];
...

这很好,因为:

  1. 你可以处理的动作,你也可以定义提示
  2. 毫无疑问,UIAlertView是什么提示了这个。(不检查UIAlertView标题或标签,这两个都是典型的方法)。

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

下面是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

另见增强的UIAlertView

使用标签属性

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

这个例子假设是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
   }
}

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

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

最新更新