GithubHelp home page GithubHelp logo

runt18 / uialertview-blocks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryanmaxwell/uialertview-blocks

0.0 2.0 0.0 510 KB

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

License: MIT License

Objective-C 95.81% Ruby 4.19%

uialertview-blocks's Introduction

UIAlertView+Blocks

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"
         cancelButtonTitle:@"Cancel"
         otherButtonTitles:@[@"Beer", @"Wine"]
                  tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                      if (buttonIndex == [alertView cancelButtonIndex]) {
                          NSLog(@"Cancelled");
                      } 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"
                                            delegate:self
                                   cancelButtonTitle:@"Cancel"
                                   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.

Usage

Add UIAlertView+Blocks.h/m into your project, or pod 'UIAlertView+Blocks' using CocoaPods. In your code, either #import <UIAlertView+Blocks/UIAlertView+Blocks.h> (Objective-C header), @import UIAlertView_Blocks; (Objective-C module), or import UIAlertView_Blocks (Swift).

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.

uialertview-blocks's People

Contributors

calt avatar karlvr avatar

Watchers

 avatar  avatar

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.