PageRenderTime 35ms CodeModel.GetById 14ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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