Coder Social home page Coder Social logo

floresrobles / rmuniversalalert Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gabmarfer/rmuniversalalert

0.0 1.0 0.0 44 KB

Wrapper class for UIAlertView / UIActionSheet / UIAlertController for targeting all iOS versions.

License: MIT License

Objective-C 97.68% Ruby 2.32%

rmuniversalalert's Introduction

RMUniversalAlert

Wrapper class for UIAlertView / UIActionSheet / UIAlertController for targeting all iOS versions.

RMUniversalAlert is a wrapper class that builds upon UIAlertView+Blocks, UIActionSheet+Blocks, and UIAlertController+Blocks, and gives a simplified interface to support all iOS versions.

Interface

typedef void(^RMUniversalAlertCompletionBlock)(RMUniversalAlert *alert, NSInteger buttonIndex);

Alert Views

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

Action Sheets

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

Usage - Alert Views

The below code will show an alert on all iOS versions, and allow you to perform your logic in a single inline code path. On iOS 8 and above, it will use UIAlertController - giving you red text on the destructive button. On iOS 7 and earlier, it will use a standard UIAlertView.

Objective-C

[RMUniversalAlert showAlertInViewController:self
                                  withTitle:@"Test Alert"
                                    message:@"Test Message"
                          cancelButtonTitle:@"Cancel"
                     destructiveButtonTitle:@"Delete"
                          otherButtonTitles:@[@"First Other", @"Second Other"]
                                   tapBlock:^(RMUniversalAlert *alert, NSInteger buttonIndex){
                                       if (buttonIndex == alert.cancelButtonIndex) {
                                           NSLog(@"Cancel Tapped");
                                       } else if (buttonIndex == alert.destructiveButtonIndex) {
                                           NSLog(@"Delete Tapped");
                                       } else if (buttonIndex >= alert.firstOtherButtonIndex) {
                                           NSLog(@"Other Button Index %ld", (long)buttonIndex - alert.firstOtherButtonIndex);
                                       }
                                   }];

Swift

RMUniversalAlert.showAlertInViewController(self,
    withTitle: "Test Alert",
    message: "Test Message",
    cancelButtonTitle: "Cancel",
    destructiveButtonTitle: "Delete",
    otherButtonTitles: ["First Other", "Second Other"],
    tapBlock: {(alert, buttonIndex) in
        if (buttonIndex == alert.cancelButtonIndex) {
            println("Cancel Tapped")
        } else if (buttonIndex == alert.destructiveButtonIndex) {
            println("Delete Tapped")
        } else if (buttonIndex >= alert.firstOtherButtonIndex) {
            println("Other Button Index \(buttonIndex - alert.firstOtherButtonIndex)")
        }
    })

Requirements

iOS 6 and later. Since version 0.7 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 - Action Sheet

The below code will show an action sheet on all iOS versions, and allow you to perform your logic in a single inline code path. On iOS 8 and above, it will use UIAlertController - giving you red text on the destructive button. On iOS 7 and earlier, it will use a standard UIActionSheet.

The popoverPresentationControllerBlock allows you to configure the popover's source view/rect/bar button item if the action sheet will be on an iPad.

Objective-C

[RMUniversalAlert showActionSheetInViewController:self
                                        withTitle:@"Test Action Sheet"
                                          message:@"Test Message"
                                cancelButtonTitle:@"Cancel"
                           destructiveButtonTitle:@"Delete"
                                otherButtonTitles:@[@"First Other", @"Second Other"]
               popoverPresentationControllerBlock:^(RMPopoverPresentationController *popover){
                                             popover.sourceView = self.view;
                                             popover.sourceRect = sender.frame;
                                         }
                                         tapBlock:^(RMUniversalAlert *alert, NSInteger buttonIndex){
                                             if (buttonIndex == alert.cancelButtonIndex) {
                                                 NSLog(@"Cancel Tapped");
                                             } else if (buttonIndex == alert.destructiveButtonIndex) {
                                                 NSLog(@"Delete Tapped");
                                             } else if (buttonIndex >= alert.firstOtherButtonIndex) {
                                                 NSLog(@"Other Button Index %ld", (long)buttonIndex - alert.firstOtherButtonIndex);
                                             }
                                         }];

Swift

RMUniversalAlert.showActionSheetInViewController(self,
    withTitle: "Test Action Sheet",
    message: "Test Message",
    cancelButtonTitle: "Cancel",
    destructiveButtonTitle: "Delete",
    otherButtonTitles: ["First Other", "Second Other"],
    popoverPresentationControllerBlock: {(popover) in
        popover.sourceView = self.view
        popover.sourceRect = sender.frame
    },
    tapBlock: {(alert, buttonIndex) in
        if (buttonIndex == alert.cancelButtonIndex) {
            println("Cancel Tapped")
        } else if (buttonIndex == alert.destructiveButtonIndex) {
            println("Delete Tapped")
        } else if (buttonIndex >= alert.firstOtherButtonIndex) {
            println("Other Button Index \(buttonIndex - alert.firstOtherButtonIndex)")
        }
    })

Installation

pod 'RMUniversalAlert' using CocoaPods.

Examples

Download this project, navigate to Tests and run pod install. Open RMUniversalAlert.xcworkspace.

rmuniversalalert's People

Contributors

ryanmaxwell avatar dhepper avatar ganchiku avatar yusuga avatar

Watchers

floresrobles 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.