GithubHelp home page GithubHelp logo

cyioser / uialertcontroller-blocks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryanmaxwell/uialertcontroller-blocks

0.0 1.0 0.0 50 KB

Convenience methods for UIAlertController

License: MIT License

Objective-C 97.02% Ruby 2.98%

uialertcontroller-blocks's Introduction

UIAlertController+Blocks

Convenience methods for UIAlertController. The API follows the same pattern as UIAlertView+Blocks and UIActionSheet+Blocks

Create and show an alert controller with a single call:

Objective-C

[UIAlertController showAlertInViewController:self
                                   withTitle:@"Test Alert"
                                     message:@"Test Message"
                           cancelButtonTitle:@"Cancel"
                      destructiveButtonTitle:@"Delete"
                           otherButtonTitles:@[@"First Other", @"Second Other"]
                                    tapBlock:^(UIAlertController *controller, UIAlertAction *action, NSInteger buttonIndex){

                                         if (buttonIndex == controller.cancelButtonIndex) {
                                             NSLog(@"Cancel Tapped");
                                         } else if (buttonIndex == controller.destructiveButtonIndex) {
                                             NSLog(@"Delete Tapped");
                                         } else if (buttonIndex >= controller.firstOtherButtonIndex) {
                                             NSLog(@"Other Button Index %ld", (long)buttonIndex - controller.firstOtherButtonIndex);
                                         }
                                     }];

Swift

UIAlertController.showAlert(
    in: self,
    withTitle: "Test Alert",
    message: "Test Message",
    cancelButtonTitle: "Cancel",
    destructiveButtonTitle: "Delete",
    otherButtonTitles: ["First Other", "Second Other"],
    tap: {(controller, action, buttonIndex) in
        if (buttonIndex == controller.cancelButtonIndex) {
            print("Cancel Tapped")
         } else if (buttonIndex == controller.destructiveButtonIndex) {
            print("Delete Tapped")
         } else if (buttonIndex >= controller.firstOtherButtonIndex) {
            print("Other Button Index \(buttonIndex - controller.firstOtherButtonIndex)")
         }
    }
)

Explicitly create alerts or action sheets with

+ (instancetype)showAlertInViewController:(UIViewController *)viewController
                                withTitle:(NSString *)title
                                  message:(NSString *)message
                        cancelButtonTitle:(NSString *)cancelButtonTitle
                   destructiveButtonTitle:(NSString *)destructiveButtonTitle
                        otherButtonTitles:(NSArray *)otherButtonTitles
                                 tapBlock:(UIAlertControllerCompletionBlock)tapBlock;

and

+ (instancetype)showActionSheetInViewController:(UIViewController *)viewController
                                      withTitle:(NSString *)title
                                        message:(NSString *)message
                              cancelButtonTitle:(NSString *)cancelButtonTitle
                         destructiveButtonTitle:(NSString *)destructiveButtonTitle
                              otherButtonTitles:(NSArray *)otherButtonTitles
             popoverPresentationControllerBlock:(void(^)(UIPopoverPresentationController *popover))popoverPresentationControllerBlock
                                       tapBlock:(UIAlertControllerCompletionBlock)tapBlock;

Requirements

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 UIAlertController+Blocks.h/m into your project, or pod 'UIAlertController+Blocks' using CocoaPods. In your code, either #import <UIAlertController+Blocks/UIAlertController+Blocks.h> (Objective-C header), @import UIAlertController_Blocks; (Objective-C module), or import UIAlertController_Blocks (Swift).

Supporting < iOS 8

You can add this pod to your project, along with 'UIAlertView+Blocks' or 'UIActionSheet+Blocks', and as long as you build with the iOS 8 SDK or greater, instantiate the appropriate class at runtime. e.g.

void(^deleteSalesOrderBlock)(void) = ^{
    /* Delete the sales order here */
};

if ([UIAlertController class]) {
    
    /* UIAlertController is preferable on >= iOS 8, as the destructive button will be in red */
    
    [UIAlertController showAlertInViewController:self
                                       withTitle:NSLocalizedString(@"AlertTitleDeleteSalesOrder", nil)
                                         message:NSLocalizedString(@"AlertMessageDeleteSalesOrder", nil)
                               cancelButtonTitle:NSLocalizedString(@"AlertButtonTitleCancel", nil)
                          destructiveButtonTitle:NSLocalizedString(@"AlertButtonTitleDelete", nil)
                               otherButtonTitles:nil
                                        tapBlock:^(UIAlertController *controller, UIAlertAction *action, NSInteger buttonIndex){
                                            if (buttonIndex == controller.destructiveButtonIndex) {
                                                deleteSalesOrderBlock();
                                            }
                                        }];
    
    
} else {
    [UIAlertView showWithTitle:NSLocalizedString(@"AlertTitleDeleteSalesOrder", nil)
                       message:NSLocalizedString(@"AlertMessageDeleteSalesOrder", nil)
             cancelButtonTitle:NSLocalizedString(@"AlertButtonTitleCancel", nil)
             otherButtonTitles:@[NSLocalizedString(@"AlertButtonTitleDelete", nil)]
                      tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex){
                          if (buttonIndex == alertView.firstOtherButtonIndex) {
                              deleteSalesOrderBlock();
                          }
                      }];
}

I have also created the wrapper class RMUniversalAlert which can do the above automatically for you.

uialertcontroller-blocks's People

Contributors

ryanmaxwell avatar fedser 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.