Skip to content

Commit

Permalink
Add shouldEnableFirstOtherButtonBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Maxwell committed Sep 11, 2013
1 parent 0c87596 commit 2e78a78
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ typedef void (^UIAlertViewCompletionBlock) (UIAlertView *alertView, NSInteger bu
@property (copy, nonatomic) UIAlertViewBlock willPresentBlock;
@property (copy, nonatomic) UIAlertViewBlock didPresentBlock;
@property (copy, nonatomic) UIAlertViewBlock cancelBlock;

@property (copy, nonatomic) BOOL(^shouldEnableFirstOtherButtonBlock)(UIAlertView *alertView);
```
You can create and show an alert in a single call, e.g.
Expand Down Expand Up @@ -58,6 +60,10 @@ av.cancelBlock = ^(UIAlertView *alertView) {
NSLog(@"Cancelled.");
};

av.shouldEnableFirstOtherButtonBlock = ^BOOL(UIAlertView *alertView) {
return ([[[alertView textFieldAtIndex:1] text] length] > 0);
};

[av show];
```
Expand Down
29 changes: 22 additions & 7 deletions Tests/UIAlertViewBlocks/TestAlertViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,28 @@ - (IBAction)showAlert:(id)sender;
@implementation TestAlertViewController

- (IBAction)showAlert:(id)sender {
[UIAlertView showWithTitle:@"Test"
message:@"Test Message"
cancelButtonTitle:@"Cancel"
otherButtonTitles:@[@"One", @"Two"]
tapBlock:^(UIAlertView *alertView, NSInteger index){
NSLog(@"Tapped '%@' at index %d", [alertView buttonTitleAtIndex:index], index);
}];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Sign in to my awesome service"
message:@"I promise I won’t steal your password"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];

av.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

av.tapBlock = ^(UIAlertView *alertView, NSInteger buttonIndex) {
NSLog(@"Username: %@", [[alertView textFieldAtIndex:0] text]);
NSLog(@"Password: %@", [[alertView textFieldAtIndex:1] text]);
};

av.cancelBlock = ^(UIAlertView *alertView) {
NSLog(@"Cancelled.");
};

av.shouldEnableFirstOtherButtonBlock = ^BOOL(UIAlertView *alertView){
return ([[[alertView textFieldAtIndex:1] text] length] > 0);
};

[av show];
}

@end
2 changes: 2 additions & 0 deletions UIAlertView+Blocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ typedef void (^UIAlertViewCompletionBlock) (UIAlertView *alertView, NSInteger bu
@property (copy, nonatomic) UIAlertViewBlock didPresentBlock;
@property (copy, nonatomic) UIAlertViewBlock cancelBlock;

@property (copy, nonatomic) BOOL(^shouldEnableFirstOtherButtonBlock)(UIAlertView *alertView);

@end
29 changes: 22 additions & 7 deletions UIAlertView+Blocks.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

#import <objc/runtime.h>

static const void *UIAlertViewOriginalDelegateKey = &UIAlertViewOriginalDelegateKey;
static const void *UIAlertViewOriginalDelegateKey = &UIAlertViewOriginalDelegateKey;

static const void *UIAlertViewTapBlockKey = &UIAlertViewTapBlockKey;
static const void *UIAlertViewWillPresentBlockKey = &UIAlertViewWillPresentBlockKey;
static const void *UIAlertViewDidPresentBlockKey = &UIAlertViewDidPresentBlockKey;
static const void *UIAlertViewWillDismissBlockKey = &UIAlertViewWillDismissBlockKey;
static const void *UIAlertViewDidDismissBlockKey = &UIAlertViewDidDismissBlockKey;
static const void *UIAlertViewCancelBlockKey = &UIAlertViewCancelBlockKey;
static const void *UIAlertViewTapBlockKey = &UIAlertViewTapBlockKey;
static const void *UIAlertViewWillPresentBlockKey = &UIAlertViewWillPresentBlockKey;
static const void *UIAlertViewDidPresentBlockKey = &UIAlertViewDidPresentBlockKey;
static const void *UIAlertViewWillDismissBlockKey = &UIAlertViewWillDismissBlockKey;
static const void *UIAlertViewDidDismissBlockKey = &UIAlertViewDidDismissBlockKey;
static const void *UIAlertViewCancelBlockKey = &UIAlertViewCancelBlockKey;
static const void *UIAlertViewShouldEnableFirstOtherButtonBlockKey = &UIAlertViewShouldEnableFirstOtherButtonBlockKey;

@implementation UIAlertView (Blocks)

Expand Down Expand Up @@ -110,6 +111,15 @@ - (void)setCancelBlock:(UIAlertViewBlock)cancelBlock {
objc_setAssociatedObject(self, UIAlertViewCancelBlockKey, cancelBlock, OBJC_ASSOCIATION_COPY);
}

- (void)setShouldEnableFirstOtherButtonBlock:(BOOL(^)(UIAlertView *alertView))shouldEnableFirstOtherButtonBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewShouldEnableFirstOtherButtonBlockKey, shouldEnableFirstOtherButtonBlock, OBJC_ASSOCIATION_COPY);
}

- (BOOL(^)(UIAlertView *alertView))shouldEnableFirstOtherButtonBlock {
return objc_getAssociatedObject(self, UIAlertViewShouldEnableFirstOtherButtonBlockKey);
}

#pragma mark - UIAlertViewDelegate

- (void)willPresentAlertView:(UIAlertView *)alertView {
Expand Down Expand Up @@ -192,6 +202,11 @@ - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)
}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
BOOL(^shouldEnableFirstOtherButtonBlock)(UIAlertView *alertView) = alertView.shouldEnableFirstOtherButtonBlock;

if (shouldEnableFirstOtherButtonBlock) {
return shouldEnableFirstOtherButtonBlock(alertView);
}

id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertViewShouldEnableFirstOtherButton:)]) {
Expand Down

0 comments on commit 2e78a78

Please sign in to comment.