Need help with UIAlertView-Blocks?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

434 Stars 59 Forks MIT License 68 Commits 5 Opened issues


Category on UIAlertView to use inline block callbacks instead of delegate callbacks.

Services available


Need anything else?

Contributors list

# 21,559
1 commit
# 332,833
1 commit


Category on UIAlertView to use inline block callbacks instead of delegate callbacks.

UIAlertView was created in a time before blocks, ARC, and judging by its naming – touch screens too. Who “clicks” on an alert view anyway?

Lets modernize this shizzle with some blocks goodness.

typedef void (^UIAlertViewBlock) (UIAlertView *alertView);
typedef void (^UIAlertViewCompletionBlock) (UIAlertView *alertView, NSInteger buttonIndex);

@property (copy, nonatomic) UIAlertViewCompletionBlock tapBlock; @property (copy, nonatomic) UIAlertViewCompletionBlock willDismissBlock; @property (copy, nonatomic) UIAlertViewCompletionBlock didDismissBlock;

@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.

[UIAlertView showWithTitle:@"Drink Selection"
                   message:@"Choose a refreshing beverage"
         otherButtonTitles:@[@"Beer", @"Wine"]
                  tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                      if (buttonIndex == [alertView cancelButtonIndex]) {
                      } else if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Beer"]) {
                          NSLog(@"Have a cold beer");
                      } else if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Wine"]) {
                          NSLog(@"Have a glass of chardonnay");

If you need further customization, you can create and configure an alert as you usually would, and then assign blocks to the alert, e.g.

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Sign in to my awesome service"
                                             message:@"I promise I won’t steal your password"
                                   otherButtonTitles:@"OK", nil];

av.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

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

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

[av show];

If a delegate was set on the alert view, the delegate will be preserved and the blocks will be executed before the delegate is called.

Category Requirements

Blocks - so iOS 4.0 and later. Compatible with both ARC and traditional retain/release code. Since version 0.9 the headers use the new Objective-C nullability annotations for nicer interoperability with Swift, so you will need Xcode 6.3 or later to compile it.

Test Project Requirements

The Xcode test project uses the XCTest framework and so requires >= Xcode 5.



into your project, or
pod 'UIAlertView+Blocks'
using CocoaPods. In your code, either
(Objective-C header),
@import UIAlertView_Blocks;
(Objective-C module), or
import UIAlertView_Blocks

Action Sheets

If you’d like similar functionality on UIActionSheet too, check out twin-sister UIActionSheet+Blocks.

iOS 8 and UIAlertController

Check out UIAlertController+Blocks if you would like to migrate to UIAlertController, and use a familiar API.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.