Latest 0.1.1
Homepage https://github.com/mysterioustrousers/MTBlockAlertView
License MIT
Platforms ios 5.0, requires ARC
Authors ,

An iOS Alert View that uses block-based delegation instead of protocols.

Installation

  1. Preferred method is through CocoaPods, add pod 'MTBlockAlertView' to your Podfile.
  2. Copy MTBlockAlertView.h and MTBlockAlertView.m from the MTBlockAlertView target into your project.

Usage

Instead of conforming to the UIAlertViewDelegate protocol, use blocks instead:

void (^completionHandler)(UIAlertView *, NSInteger) = ^(UIAlertView *alertView, NSInteger buttonIndex) {
    if (buttonIndex == alertView.cancelButtonIndex) {
        [MTBlockAlertView showWithTitle:@"Cancel" message:@"You tapped Cancel"];
    } else {
        [MTBlockAlertView showWithTitle:@"OK" message:@"You tapped OK"];
    }
};

MTBlockAlertView *alertView = [[MTBlockAlertView alloc] initWithTitle:@"Init Version"
                                                              message:@"You tapped the init version"
                                                    completionHanlder:completionHandler
                                                    cancelButtonTitle:@"Cancel"
                                                    otherButtonTitles:@"OK", nil];

Aside from the convenience of not having to conform to the protocol, you also have the benefit of each MTBlockAlertView instance being in charge of its own dismissal. Anyone who has tried to use the delegate methods of multiple UIAlertViews in the same view controller knows the frustration of it.

We also provide several class convenience methods for common use cases. Run the MTBlockAlertViewDemo target to see some examples.

Super-simple

[MTBlockAlertView showWithTitle:@"Cool Message" message:@"This is a really cool message"];

A little more control

[MTBlockAlertView showWithTitle:@"Cool Message"
                        message:@"Tapping this will do something cool"
                completionBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    // Do some work
                }];

Full-monty

[MTBlockAlertView showWithTitle:@"Cool Message"
                        message:@"I like blocks."
              cancelButtonTitle:@"Cancel"
               otherButtonTitle:@"OK"
                 alertViewStyle:UIAlertViewStylePlainTextInput
                completionBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    // Do some work
                }];

Contributors

@parkerwightman (@parkerwightman)

Latest podspec

{
    "name": "MTBlockAlertView",
    "version": "0.1.1",
    "summary": "An iOS Alert View that uses block-based delegation instead of protocols.",
    "homepage": "https://github.com/mysterioustrousers/MTBlockAlertView",
    "license": "MIT",
    "authors": {
        "Mysterious Trousers, LLC": "[email protected]",
        "Parker Wightman": "[email protected]"
    },
    "source": {
        "git": "https://github.com/mysterioustrousers/MTBlockAlertView.git",
        "tag": "0.1.1"
    },
    "platforms": {
        "ios": "5.0"
    },
    "source_files": "MTBlockAlertView/MTBlockAlertView/*.{h,m}",
    "requires_arc": true
}

Pin It on Pinterest

Share This