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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
//
// UIActionSheet+BlocksKit.h
// BlocksKit
//
#import "BKGlobals.h"
/** UIActionSheet without delegates!
This set of extensions and convenience classes allows
for an instance of UIAlertView without the implementation
of a delegate. Any time you instantiate a UIAlertView
using the methods here, you must add buttons using
addButtonWithTitle:handler: to make sure nothing breaks.
A typical invocation might go like this:
UIActionSheet *testSheet = [UIActionSheet actionSheetWithTitle:@"Please select one."];
[testSheet addButtonWithTitle:@"Zip" handler:^{ NSLog(@"Zip!"); }];
[testSheet addButtonWithTitle:@"Zap" handler:^{ NSLog(@"Zap!"); }];
[testSheet addButtonWithTitle:@"Zop" handler:^{ NSLog(@"Zop!"); }];
[testSheet setDestructiveButtonWithTitle:@"No!" handler:^{ NSLog(@"Fine!"); }];
[testSheet setCancelButtonWithTitle:nil handler:^{ NSLog(@"Never mind, then!"); }];
[testSheet showInView:self.view];
Includes code by the following:
- [Landon Fuller](http://landonf.bikemonkey.org), "Using Blocks".
- [Peter Steinberger](https://github.com/steipete)
- [Zach Waldowski](https://github.com/zwaldowski)
@warning UIActionSheet is only available on a platform with UIKit.
*/
@interface UIActionSheet (BlocksKit) <UIActionSheetDelegate>
///-----------------------------------
/// @name Creating action sheets
///-----------------------------------
/** Creates and returns a new action sheet with only a title and cancel button.
@param title The header of the action sheet.
@return A newly created action sheet.
*/
+ (id)actionSheetWithTitle:(NSString *)title;
/** Returns a configured action sheet with only a title and cancel button.
@param title The header of the action sheet.
@return An instantiated actionSheet.
*/
- (id)initWithTitle:(NSString *)title;
///-----------------------------------
/// @name Adding buttons
///-----------------------------------
/** Add a new button with an associated code block.
@param title The text of the button.
@param block A block of code.
*/
- (NSInteger)addButtonWithTitle:(NSString *)title handler:(BKBlock)block;
/** Set the destructive (red) button with an associated code block.
@warning Because buttons cannot be removed from an action sheet,
be aware that the effects of calling this method are cumulative.
Previously added destructive buttons will become normal buttons.
@param title The text of the button.
@param block A block of code.
*/
- (NSInteger)setDestructiveButtonWithTitle:(NSString *)title handler:(BKBlock)block;
/** Set the title and trigger of the cancel button.
`block` can be set to `nil`, but this is generally useless as
the cancel button is configured already to do nothing.
iPhone users will have the button shown regardless; if the title is
set to `nil`, it will automatically be localized.
@param title The text of the button.
@param block A block of code.
*/
- (NSInteger)setCancelButtonWithTitle:(NSString *)title handler:(BKBlock)block;
///-----------------------------------
/// @name Altering actions
///-----------------------------------
/** Sets the block that is to be fired when a button is pressed.
@param block A code block, or nil to set no response.
@param index The index of a button already added to the action sheet.
*/
- (void)setHandler:(BKBlock)block forButtonAtIndex:(NSInteger)index;
/** The block that is to be fired when a button is pressed.
@param index The index of a button already added to the action sheet.
@return A code block, or nil if no block is assigned.
*/
- (BKBlock)handlerForButtonAtIndex:(NSInteger)index;
/** The block to be fired when the action sheet is dismissed with the cancel
button and/or action.
This property performs the same action as setCancelButtonWithTitle:handler:
but with `title` set to nil. Contrary to setCancelButtonWithTitle:handler:,
you can set this property multiple times and multiple cancel buttons will
not be generated.
*/
@property (nonatomic, copy) BKBlock cancelBlock;
/** The block to be fired before the action sheet will show. */
@property (nonatomic, copy) void (^willShowBlock)(UIActionSheet *);
/** The block to be fired when the action sheet shows. */
@property (nonatomic, copy) void (^didShowBlock)(UIActionSheet *);
/** The block to be fired before the action sheet will dismiss. */
@property (nonatomic, copy) void (^willDismissBlock)(UIActionSheet *, NSInteger);
/** The block to be fired after the action sheet dismisses. */
@property (nonatomic, copy) void (^didDismissBlock)(UIActionSheet *, NSInteger);
@end
|