UIAlertView+BlocksKit.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // UIAlertView+BlocksKit.h
  3. // BlocksKit
  4. //
  5. #import <UIKit/UIKit.h>
  6. /** UIAlertView without delegates!
  7. This set of extensions and convenience classes allows
  8. for an instance of UIAlertView without the implementation
  9. of a delegate. Any time you instantiate a UIAlertView
  10. using the methods here, you must add buttons using
  11. addButtonWithTitle:handler:otherwise nothing will happen.
  12. A typical invocation will go like this:
  13. UIAlertView *testView = [UIAlertView alertViewWithTitle:@"Application Alert" message:@"This app will explode in 42 seconds."];
  14. [testView setCancelButtonWithTitle:@"Oh No!" handler:^{ NSLog(@"Boom!"); }];
  15. [testView show];
  16. A more traditional, and more useful, modal dialog looks like so:
  17. UIAlertView *testView = [UIAlertView alertViewWithTitle:@"Very important!" message:@"Do you like chocolate?"];
  18. [testView addButtonWithTitle:@"Yes" handler:^{ NSLog(@"Yay!"); }];
  19. [testView addButtonWithTitle:@"No" handler:^{ NSLog(@"We hate you."); }];
  20. [testView show];
  21. Includes code by the following:
  22. - [Landon Fuller](http://landonf.bikemonkey.org), "Using Blocks".
  23. - [Peter Steinberger](https://github.com/steipete)
  24. - [Zach Waldowski](https://github.com/zwaldowski)
  25. @warning UIAlertView is only available on a platform with UIKit.
  26. */
  27. @interface UIAlertView (BlocksKit)
  28. ///-----------------------------------
  29. /// @name Creating alert views
  30. ///-----------------------------------
  31. /** Creates and shows a new alert view with only a title, message, and cancel button.
  32. @param title The title of the alert view.
  33. @param message The message content of the alert.
  34. @param cancelButtonTitle The title of the cancel button. If both cancelButtonTitle and otherButtonTitles are empty or nil, defaults to a
  35. @param otherButtonTitles Titles of additional buttons to add to the receiver.
  36. @param block A block of code to be fired on the dismissal of the alert view.
  37. @return The UIAlertView.
  38. */
  39. + (UIAlertView*)bk_showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles handler:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))block;
  40. /** Creates and returns a new alert view with only a title and cancel button.
  41. @param title The title of the alert view.
  42. @return A newly created alert view.
  43. */
  44. + (id)bk_alertViewWithTitle:(NSString *)title;
  45. /** Creates and returns a new alert view with only a title, message, and cancel button.
  46. @param title The title of the alert view.
  47. @param message The message content of the alert.
  48. @return A newly created alert view.
  49. */
  50. + (id)bk_alertViewWithTitle:(NSString *)title message:(NSString *)message;
  51. /** Returns a configured alert view with only a title, message, and cancel button.
  52. @param title The title of the alert view.
  53. @param message The message content of the alert.
  54. @return An instantiated alert view.
  55. */
  56. - (id)bk_initWithTitle:(NSString *)title message:(NSString *)message NS_REPLACES_RECEIVER;
  57. ///-----------------------------------
  58. /// @name Adding buttons
  59. ///-----------------------------------
  60. /** Add a new button with an associated code block.
  61. @param title The text of the button.
  62. @param block A block of code.
  63. */
  64. - (NSInteger)bk_addButtonWithTitle:(NSString *)title handler:(void (^)(void))block;
  65. /** Set the title and trigger of the cancel button.
  66. @param title The text of the button.
  67. @param block A block of code.
  68. */
  69. - (NSInteger)bk_setCancelButtonWithTitle:(NSString *)title handler:(void (^)(void))block;
  70. ///-----------------------------------
  71. /// @name Altering actions
  72. ///-----------------------------------
  73. /** Sets the block that is to be fired when a button is pressed.
  74. @param block A code block, or nil to set no response.
  75. @param index The index of a button already added to the action sheet.
  76. */
  77. - (void)bk_setHandler:(void (^)(void))block forButtonAtIndex:(NSInteger)index;
  78. /** The block that is to be fired when a button is pressed.
  79. @param index The index of the button already added to the alert view.
  80. @return A code block, or nil if no block yet assigned.
  81. */
  82. - (void (^)(void))bk_handlerForButtonAtIndex:(NSInteger)index;
  83. /** The block to be fired when the action sheet is dismissed with the cancel
  84. button.
  85. Contrary to setCancelButtonWithTitle:handler:, you can set this
  86. property multiple times but multiple cancel buttons will
  87. not be generated.
  88. */
  89. @property (nonatomic, copy, setter = bk_setCancelBlock:) void (^bk_cancelBlock)(void);
  90. /** The block to be fired before the alert view will show. */
  91. @property (nonatomic, copy, setter = bk_setWillShowBlock:) void (^bk_willShowBlock)(UIAlertView *alertView);
  92. /** The block to be fired when the alert view shows. */
  93. @property (nonatomic, copy, setter = bk_setDidShowBlock:) void (^bk_didShowBlock)(UIAlertView *alertView);
  94. /** The block to be fired before the alert view will dismiss. */
  95. @property (nonatomic, copy, setter = bk_setWillDismissBlock:) void (^bk_willDismissBlock)(UIAlertView *alertView, NSInteger buttonIndex);
  96. /** The block to be fired after the alert view dismisses. */
  97. @property (nonatomic, copy, setter = bk_setDidDismissBlock:) void (^bk_didDismissBlock)(UIAlertView *alertView, NSInteger buttonIndex);
  98. /** The block to be fired to determine whether the first non-cancel should be enabled */
  99. @property (nonatomic, copy, setter = bk_SetShouldEnableFirstOtherButtonBlock:) BOOL (^bk_shouldEnableFirstOtherButtonBlock)(UIAlertView *alertView) NS_AVAILABLE_IOS(5_0);
  100. @end