forked from jivadevoe/UIAlertView-Blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIActionSheet+Blocks.m
100 lines (81 loc) · 3.43 KB
/
UIActionSheet+Blocks.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// UIActionSheet+Blocks.m
// Shibui
//
// Created by Jiva DeVoe on 1/5/11.
// Copyright 2011 Random Ideas, LLC. All rights reserved.
//
#import "UIActionSheet+Blocks.h"
#import <objc/runtime.h>
static NSString *RI_BUTTON_ASS_KEY = @"com.random-ideas.BUTTONS";
static NSString *RI_DISMISSAL_ACTION_KEY = @"com.random-ideas.DISMISSAL_ACTION";
@implementation UIActionSheet (Blocks)
-(id)initWithTitle:(NSString *)inTitle cancelButtonItem:(RIButtonItem *)inCancelButtonItem destructiveButtonItem:(RIButtonItem *)inDestructiveItem otherButtonItems:(RIButtonItem *)inOtherButtonItems, ...
{
if((self = [self initWithTitle:inTitle delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]))
{
NSMutableArray *buttonsArray = [NSMutableArray array];
RIButtonItem *eachItem;
va_list argumentList;
if (inOtherButtonItems)
{
[buttonsArray addObject: inOtherButtonItems];
va_start(argumentList, inOtherButtonItems);
while((eachItem = va_arg(argumentList, RIButtonItem *)))
{
[buttonsArray addObject: eachItem];
}
va_end(argumentList);
}
for(RIButtonItem *item in buttonsArray)
{
[self addButtonWithTitle:item.label];
}
if(inDestructiveItem)
{
[buttonsArray addObject:inDestructiveItem];
NSInteger destIndex = [self addButtonWithTitle:inDestructiveItem.label];
[self setDestructiveButtonIndex:destIndex];
}
if(inCancelButtonItem)
{
[buttonsArray addObject:inCancelButtonItem];
NSInteger cancelIndex = [self addButtonWithTitle:inCancelButtonItem.label];
[self setCancelButtonIndex:cancelIndex];
}
objc_setAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY, buttonsArray, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return self;
}
- (NSInteger)addButtonItem:(RIButtonItem *)item
{
NSMutableArray *buttonsArray = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY);
NSInteger buttonIndex = [self addButtonWithTitle:item.label];
[buttonsArray addObject:item];
return buttonIndex;
}
- (void)setDismissalAction:(void(^)())dismissalAction
{
objc_setAssociatedObject(self, (__bridge const void *)RI_DISMISSAL_ACTION_KEY, nil, OBJC_ASSOCIATION_COPY);
objc_setAssociatedObject(self, (__bridge const void *)RI_DISMISSAL_ACTION_KEY, dismissalAction, OBJC_ASSOCIATION_COPY);
}
- (void(^)())dismissalAction
{
return objc_getAssociatedObject(self, (__bridge const void *)RI_DISMISSAL_ACTION_KEY);
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
// Action sheets pass back -1 when they're cleared for some reason other than a button being
// pressed.
if (buttonIndex >= 0)
{
NSArray *buttonsArray = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY);
RIButtonItem *item = [buttonsArray objectAtIndex:buttonIndex];
if(item.action)
item.action();
}
if (self.dismissalAction) self.dismissalAction();
objc_setAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, (__bridge const void *)RI_DISMISSAL_ACTION_KEY, nil, OBJC_ASSOCIATION_COPY);
}
@end