/BlocksKit/UIActionSheet+BlocksKit.m

http://github.com/zwaldowski/BlocksKit · Objective C · 162 lines · 119 code · 39 blank · 4 comment · 23 complexity · 6fb23c84f2247d25915d9ee161ca9608 MD5 · raw file

  1. //
  2. // UIActionSheet+BlocksKit.m
  3. // BlocksKit
  4. //
  5. #import "UIActionSheet+BlocksKit.h"
  6. #import "A2BlockDelegate+BlocksKit.h"
  7. #pragma mark Custom delegate
  8. @interface A2DynamicUIActionSheetDelegate : A2DynamicDelegate
  9. @end
  10. @implementation A2DynamicUIActionSheetDelegate
  11. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  12. id realDelegate = self.realDelegate;
  13. if (realDelegate && [realDelegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)])
  14. [realDelegate actionSheet:actionSheet clickedButtonAtIndex:buttonIndex];
  15. id key = [NSNumber numberWithInteger:buttonIndex];
  16. BKBlock block = [self.handlers objectForKey:key];
  17. if (block)
  18. block();
  19. }
  20. - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
  21. id realDelegate = self.realDelegate;
  22. if (realDelegate && [realDelegate respondsToSelector:@selector(willPresentActionSheet:)])
  23. [realDelegate willPresentActionSheet:actionSheet];
  24. void (^block)(UIActionSheet *) = [self blockImplementationForMethod:_cmd];
  25. if (block)
  26. block(actionSheet);
  27. }
  28. - (void)didPresentActionSheet:(UIActionSheet *)actionSheet {
  29. id realDelegate = self.realDelegate;
  30. if (realDelegate && [realDelegate respondsToSelector:@selector(didPresentActionSheet:)])
  31. [realDelegate didPresentActionSheet:actionSheet];
  32. void (^block)(UIActionSheet *) = [self blockImplementationForMethod:_cmd];
  33. if (block)
  34. block(actionSheet);
  35. }
  36. - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
  37. id realDelegate = self.realDelegate;
  38. if (realDelegate && [realDelegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)])
  39. [realDelegate actionSheet:actionSheet willDismissWithButtonIndex:buttonIndex];
  40. void (^block)(UIActionSheet *, NSInteger) = [self blockImplementationForMethod:_cmd];
  41. if (block)
  42. block(actionSheet, buttonIndex);
  43. }
  44. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
  45. id realDelegate = self.realDelegate;
  46. if (realDelegate && [realDelegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)])
  47. [realDelegate actionSheet:actionSheet didDismissWithButtonIndex:buttonIndex];
  48. void (^block)(UIActionSheet *, NSInteger) = [self blockImplementationForMethod:_cmd];
  49. if (block)
  50. block(actionSheet, buttonIndex);
  51. }
  52. - (void)actionSheetCancel:(UIActionSheet *)actionSheet {
  53. id realDelegate = self.realDelegate;
  54. if (realDelegate && [realDelegate respondsToSelector:@selector(actionSheetCancel:)])
  55. [realDelegate actionSheetCancel:actionSheet];
  56. BKBlock block = actionSheet.cancelBlock;
  57. if (block)
  58. block();
  59. }
  60. @end
  61. #pragma mark - Category
  62. @implementation UIActionSheet (BlocksKit)
  63. @dynamic willShowBlock, didShowBlock, willDismissBlock, didDismissBlock;
  64. + (void)load {
  65. @autoreleasepool {
  66. [self registerDynamicDelegate];
  67. NSDictionary *methods = [NSDictionary dictionaryWithObjectsAndKeys:
  68. @"willPresentActionSheet:", @"willShowBlock",
  69. @"didPresentActionSheet:", @"didShowBlock",
  70. @"actionSheet:willDismissWithButtonIndex:", @"willDismissBlock",
  71. @"actionSheet:didDismissWithButtonIndex:", @"didDismissBlock",
  72. nil];
  73. [self linkDelegateMethods:methods];
  74. }
  75. }
  76. #pragma mark Initializers
  77. + (id)actionSheetWithTitle:(NSString *)title {
  78. return [[[UIActionSheet alloc] initWithTitle:title] autorelease];
  79. }
  80. - (id)initWithTitle:(NSString *)title {
  81. return [self initWithTitle:title delegate:self.dynamicDelegate cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
  82. }
  83. #pragma mark Actions
  84. - (NSInteger)addButtonWithTitle:(NSString *)title handler:(BKBlock)block {
  85. NSAssert(title.length, @"A button without a title cannot be added to an action sheet.");
  86. NSInteger index = [self addButtonWithTitle:title];
  87. [self setHandler:block forButtonAtIndex:index];
  88. return index;
  89. }
  90. - (NSInteger)setDestructiveButtonWithTitle:(NSString *)title handler:(BKBlock)block {
  91. NSInteger index = [self addButtonWithTitle:title handler:block];
  92. self.destructiveButtonIndex = index;
  93. return index;
  94. }
  95. - (NSInteger)setCancelButtonWithTitle:(NSString *)title handler:(BKBlock)block {
  96. NSInteger cancelButtonIndex = self.cancelButtonIndex;
  97. if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) && !title.length)
  98. title = NSLocalizedString(@"Cancel", nil);
  99. if (title.length)
  100. cancelButtonIndex = [self addButtonWithTitle:title];
  101. [self setHandler:block forButtonAtIndex:cancelButtonIndex];
  102. self.cancelButtonIndex = cancelButtonIndex;
  103. return cancelButtonIndex;
  104. }
  105. #pragma mark Properties
  106. - (void)setHandler:(BKBlock)block forButtonAtIndex:(NSInteger)index {
  107. id key = [NSNumber numberWithInteger:index];
  108. if (block)
  109. [[self.dynamicDelegate handlers] setObject:block forKey:key];
  110. else
  111. [[self.dynamicDelegate handlers] removeObjectForKey:key];
  112. }
  113. - (BKBlock)handlerForButtonAtIndex:(NSInteger)index {
  114. id key = [NSNumber numberWithInteger:index];
  115. return [[self.dynamicDelegate handlers] objectForKey:key];
  116. }
  117. - (BKBlock)cancelBlock {
  118. return [self handlerForButtonAtIndex:self.cancelButtonIndex];
  119. }
  120. - (void)setCancelBlock:(BKBlock)block {
  121. [self setHandler:block forButtonAtIndex:self.cancelButtonIndex];
  122. }
  123. @end