搜尋此網誌

2012年9月4日 星期二

如何判斷是哪個alert


方法一
@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
   }
}
方法二
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:
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).
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)buttonIndex implementation that checked against tags / titles of UIAlertViews. :( Was no bueno.
Here is the EasyAlertView implementation:
#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

沒有留言:

張貼留言