如何确定哪个UIAlertView称为委托。

时间:2011-10-06 17:43:14

标签: iphone objective-c ios xcode

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

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

我的问题是,如何找到哪个AlertView称为此委托。

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

5 个答案:

答案 0 :(得分:19)

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

}

答案 1 :(得分:4)

在我的所有项目中,我都有一个名为EasyAlertView的类。你可以在网上找到类似的代码,但是jist最终会得到一个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标题或标签,这两种方法都是典型方法)。
  3. 请注意,如果需要,操作处理程序可以调用另一个函数来处理行为,但关键是,将行为与提示相关联非常容易。在我们使用这段代码之前,我们在UIAlertView协议- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex实现中有一些if / else语句的迷宫,它们检查了UIAlertViews的标签/标题。 :(没有bueno。

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

    另见An enhancement for UIAlertView

答案 2 :(得分:3)

使用tag属性

答案 3 :(得分:3)

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

此示例假设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
   }
}

答案 4 :(得分:2)

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

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