UIActionSheet+BlocksKit.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // UIActionSheet+BlocksKit.h
  3. // BlocksKit
  4. //
  5. #import <UIKit/UIKit.h>
  6. /** UIActionSheet without delegates!
  7. This set of extensions and convenience classes allows
  8. for an instance of UIActionSheet without the implementation
  9. of a delegate. Any time you instantiate a UIActionSheet
  10. using the methods here, you must add buttons using
  11. addButtonWithTitle:handler: to make sure nothing breaks.
  12. A typical invocation might go like this:
  13. UIActionSheet *testSheet = [UIActionSheet actionSheetWithTitle:@"Please select one."];
  14. [testSheet addButtonWithTitle:@"Zip" handler:^{ NSLog(@"Zip!"); }];
  15. [testSheet addButtonWithTitle:@"Zap" handler:^{ NSLog(@"Zap!"); }];
  16. [testSheet addButtonWithTitle:@"Zop" handler:^{ NSLog(@"Zop!"); }];
  17. [testSheet setDestructiveButtonWithTitle:@"No!" handler:^{ NSLog(@"Fine!"); }];
  18. [testSheet setCancelButtonWithTitle:nil handler:^{ NSLog(@"Never mind, then!"); }];
  19. [testSheet showInView:self.view];
  20. Includes code by the following:
  21. - [Landon Fuller](http://landonf.bikemonkey.org), "Using Blocks".
  22. - [Peter Steinberger](https://github.com/steipete)
  23. - [Zach Waldowski](https://github.com/zwaldowski)
  24. @warning UIActionSheet is only available on a platform with UIKit.
  25. */
  26. @interface UIActionSheet (BlocksKit) <UIActionSheetDelegate>
  27. ///-----------------------------------
  28. /// @name Creating action sheets
  29. ///-----------------------------------
  30. /** Creates and returns a new action sheet with only a title and cancel button.
  31. @param title The header of the action sheet.
  32. @return A newly created action sheet.
  33. */
  34. + (id)bk_actionSheetWithTitle:(NSString *)title;
  35. /** Returns a configured action sheet with only a title and cancel button.
  36. @param title The header of the action sheet.
  37. @return An instantiated actionSheet.
  38. */
  39. - (id)bk_initWithTitle:(NSString *)title NS_REPLACES_RECEIVER;
  40. ///-----------------------------------
  41. /// @name Adding buttons
  42. ///-----------------------------------
  43. /** Add a new button with an associated code block.
  44. @param title The text of the button.
  45. @param block A block of code.
  46. */
  47. - (NSInteger)bk_addButtonWithTitle:(NSString *)title handler:(void (^)(void))block;
  48. /** Set the destructive (red) button with an associated code block.
  49. @warning Because buttons cannot be removed from an action sheet,
  50. be aware that the effects of calling this method are cumulative.
  51. Previously added destructive buttons will become normal buttons.
  52. @param title The text of the button.
  53. @param block A block of code.
  54. */
  55. - (NSInteger)bk_setDestructiveButtonWithTitle:(NSString *)title handler:(void (^)(void))block;
  56. /** Set the title and trigger of the cancel button.
  57. `block` can be set to `nil`, but this is generally useless as
  58. the cancel button is configured already to do nothing.
  59. iPhone users will have the button shown regardless; if the title is
  60. set to `nil`, it will automatically be localized.
  61. @param title The text of the button.
  62. @param block A block of code.
  63. */
  64. - (NSInteger)bk_setCancelButtonWithTitle:(NSString *)title handler:(void (^)(void))block;
  65. ///-----------------------------------
  66. /// @name Altering actions
  67. ///-----------------------------------
  68. /** Sets the block that is to be fired when a button is pressed.
  69. @param block A code block, or nil to set no response.
  70. @param index The index of a button already added to the action sheet.
  71. */
  72. - (void)bk_setHandler:(void (^)(void))block forButtonAtIndex:(NSInteger)index;
  73. /** The block that is to be fired when a button is pressed.
  74. @param index The index of a button already added to the action sheet.
  75. @return A code block, or nil if no block is assigned.
  76. */
  77. - (void (^)(void))bk_handlerForButtonAtIndex:(NSInteger)index;
  78. /** The block to be fired when the action sheet is dismissed with the cancel
  79. button and/or action.
  80. This property performs the same action as setCancelButtonWithTitle:handler:
  81. but with `title` set to nil. Contrary to setCancelButtonWithTitle:handler:,
  82. you can set this property multiple times and multiple cancel buttons will
  83. not be generated.
  84. */
  85. @property (nonatomic, copy, setter = bk_setCancelBlock:) void (^bk_cancelBlock)(void);
  86. /** The block to be fired before the action sheet will show. */
  87. @property (nonatomic, copy, setter = bk_setWillShowBlock:) void (^bk_willShowBlock)(UIActionSheet *actionSheet);
  88. /** The block to be fired when the action sheet shows. */
  89. @property (nonatomic, copy, setter = bk_setDidShowBlock:) void (^bk_didShowBlock)(UIActionSheet *actionSheet);
  90. /** The block to be fired before the action sheet will dismiss. */
  91. @property (nonatomic, copy, setter = bk_setWillDismissBlock:) void (^bk_willDismissBlock)(UIActionSheet *actionSheet, NSInteger buttonIndex);
  92. /** The block to be fired after the action sheet dismisses. */
  93. @property (nonatomic, copy, setter = bk_setDidDismissBlock:) void (^bk_didDismissBlock)(UIActionSheet *actionSheet, NSInteger buttonIndex);
  94. @end