Latest 2.0.1
Homepage https://github.com/AliSoftware/OHActionSheet
License MIT
Platforms ios 4.0, requires ARC
Frameworks Foundation, UIKit
Authors

About this class

This class make it easier to use UIActionSheet with blocks.

This allows you to provide directly the code to execute (as a block) in return to the tap on a button,
instead of declaring a delegate and implementing the corresponding methods.

This also has the huge advantage of simplifying the code especially when using multiple UIActionSheets in the same object (as in such case, it is not easy to have a clean code if you share the same delegate)

Note: You may also be interested in OHAlertView

Usage Example

NSArray* flavours = [NSArray arrayWithObjects:@"chocolate",@"vanilla",@"strawberry",nil];

[OHActionSheet showSheetInView:self.window
                           title:@"Ice cream?"
               cancelButtonTitle:@"Maybe later"
          destructiveButtonTitle:@"No thanks!"
               otherButtonTitles:flavours
                      completion:^(OHActionSheet *sheet, NSInteger buttonIndex)
 {
     NSLog(@"button tapped: %d",buttonIndex);
     if (buttonIndex == sheet.cancelButtonIndex) {
         self.status.text = @"Your order has been postponed";
     } else if (buttonIndex == sheet.destructiveButtonIndex) {
         self.status.text = @"Your order has been cancelled";
     } else {
         NSString* flavour = [flavours objectAtIndex:(buttonIndex-sheet.firstOtherButtonIndex)];
         self.status.text = [NSString stringWithFormat:@"You ordered a %@ ice cream.",flavour];
     }
 }];

ActionSheets with timeout

You can also use this class to generate an ActionSheet that will be dismissed after a given time.
(You can even add a dynamic text on your sheet to display the live countdown)

NSArray* flavours = [NSArray arrayWithObjects:@"apple",@"pear",@"banana",nil];

[[[OHActionSheet alloc] initWithTitle:@"What's your favorite fruit?"
             cancelButtonTitle:@"Don't care"
        destructiveButtonTitle:nil
             otherButtonTitles:flavours
                    completion:^(OHActionSheet *sheet, NSInteger buttonIndex)
 {
     NSLog(@"button tapped: %d",buttonIndex);
     if (buttonIndex == sheet.cancelButtonIndex) {
         self.status.text = @"You didn't answer the question";
     } else if (buttonIndex == -1) {
         self.status.text = @"The action sheet timed out";
     } else {
         NSString* fruit = [flavours objectAtIndex:(buttonIndex-sheet.firstOtherButtonIndex)];
         self.status.text = [NSString stringWithFormat:@"Your favorite fruit is %@.",fruit];
     }
 }] showInView:self.window withTimeout:8 timeoutButtonIndex:-1];

Compatibility Notes

  • This class uses blocks, which is a feature introduced in iOS 4.0.
  • This class is compatible with both ARC and non-ARC projects.

License

This code is under MIT License.

CocoaPods

This class is referenced in CocoaPods, so you can simply add pod OHActionSheet to your Podfile to add it to your pods.

Latest podspec

{
    "name": "OHActionSheet",
    "platforms": {
        "ios": "4.0"
    },
    "version": "2.0.1",
    "summary": "UIActionSheet subclass that uses blocks to handle its callback (which make the code much more easier and readable).",
    "description": "                    UIActionSheet subclass that uses blocks to handle the code to execute after the user tapped a button.n                    n                    No need to implement a delegate method anymore,n                    and this makes it much more easier to manage especially when you use multiple ActionSheets in the same class.n                    n                    This class also allows you to handle ActionSheets with a timeoutn                    (it simulates a tap on the button you specify after a given number of seconds)n",
    "homepage": "https://github.com/AliSoftware/OHActionSheet",
    "license": "MIT",
    "authors": {
        "Olivier Halligon": "[email protected]"
    },
    "source": {
        "git": "https://github.com/AliSoftware/OHActionSheet.git",
        "tag": "2.0.1"
    },
    "source_files": "OHActionSheet/*.{m,h}",
    "public_header_files": "OHActionSheet/*.h",
    "frameworks": [
        "Foundation",
        "UIKit"
    ],
    "requires_arc": true
}

Pin It on Pinterest

Share This