GithubHelp home page GithubHelp logo

runt18 / uiactionsheet-blocks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryanmaxwell/uiactionsheet-blocks

0.0 1.0 0.0 342 KB

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

License: MIT License

Objective-C 96.46% Ruby 3.54%

uiactionsheet-blocks's Introduction

UIActionSheet+Blocks

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

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

Lets modernize this shizzle with some blocks goodness.

typedef void (^UIActionSheetBlock) (UIActionSheet *actionSheet);
typedef void (^UIActionSheetCompletionBlock) (UIActionSheet *actionSheet, NSInteger buttonIndex);

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

@property (copy, nonatomic) UIActionSheetBlock willPresentBlock;
@property (copy, nonatomic) UIActionSheetBlock didPresentBlock;
@property (copy, nonatomic) UIActionSheetBlock cancelBlock;

You can create and show an action sheet in a single call, e.g.

[UIActionSheet showInView:self.view
                withTitle:@"Are you sure you want to delete all the things?"
        cancelButtonTitle:@"Cancel"
   destructiveButtonTitle:@"Delete all the things"
        otherButtonTitles:@[@"Just some of the things", @"Most of the things"]
                 tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
                     NSLog(@"Chose %@", [actionSheet buttonTitleAtIndex:buttonIndex]);
                 }];

The full suite of action methods are supported, including showFromTabBar:, showFromToolbar:, showInView:, showFromBarButtonItem:animated: and showFromRect:inView:animated:.

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

UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"Choose a coffee"
                                                delegate:nil
                                       cancelButtonTitle:@"Cancel"
                                  destructiveButtonTitle:nil
                                       otherButtonTitles:@"Flat White", @"Latte", @"Cappuccino", @"Long Black", nil];

as.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

as.tapBlock = ^(UIActionSheet *actionSheet, NSInteger buttonIndex){
    NSLog(@"Chose %@", [actionSheet buttonTitleAtIndex:buttonIndex]);
};

[as showInView:self.view];

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

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.

Usage

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

Alert Views

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

iOS 8 and UIAlertController

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

uiactionsheet-blocks's People

Contributors

johnboiles avatar

Watchers

 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.