Git Product home page Git Product logo

mtblockalertview's Introduction

MTBlockAlertView

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

[Parker Wightman)(https://github.com/pwightman) (@parkerwightman)

mtblockalertview's People

Contributors

kilink avatar pitiphong-p avatar pwightman avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

xintao222

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.