Giter VIP home page Giter VIP logo

actionsheetdelegate's Introduction

ActionSheetDelegate is a simple way to create a delegate for a UIActionSheet. When creating an instance, you specify a "handler" Block whose signature matches that of the UIActionSheetDelegate method actionSheet:clickedButtonAtIndex:. ActionSheetDelegate conforms to the UIActionSheetDelegate protocol and thus implements that method.

You then create a sheet, passing your instance of ActionSheetDelegate for the delegate argument. When the sheet sends actionSheet:clickedButtonAtIndex:, the delegate simply passes the arguments on to its handler Block.

The class also has a handy method that uses the Objective-C runtime associated objects feature to tie its lifetime to its sheet's. Tying the two together this way means that there is no need to keep a local (or ivar-level) reference to the delegate; its only owner is the sheet itself, and it will be destroyed when the sheet is.

A quick example of usage:

ActionSheetDelegate * delegate;
delegate = [ActionSheetDelegate delegateWithHandler:
    ^( UIActionSheet * sheet, NSInteger idx ){
        if( idx == [sheet destructiveButtonIndex] ){
            [controller removeSomeImage];
        }
        else if( idx == [sheet firstOtherButtonIndex] ){
            [controller presentImagePicker];
        }
        // Cancel button "falls through" to no action
}];

UIActionSheet * sheet;
sheet = [[UIActionSheet alloc] initWithTitle:@""
                                    delegate:delegate
                           cancelButtonTitle:@"Cancel"
                      destructiveButtonTitle:@"Remove Image"
                           otherButtonTitles:@"Choose New Image", nil];
// Tie delegate's lifespan to that of the sheet
[delegate associateSelfWithSheet:sheet];

[sheet showInView:[self view]];

The contents of this repository are in the public domain. I retain no copyright, and they are offered without restriction or warranty. You are free to use them in whatever way you like. If you would like to mention that I created the code, it will be appreciated, but it isn't at all necessary. For further details, please see License.txt

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.