-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBActionSheet.m
133 lines (111 loc) · 3.38 KB
/
RBActionSheet.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
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
129
130
131
132
133
//
// RBActionSheet.m
// CarCamApp
//
// Created by Robbie Bykowski on 06/11/2012.
// Copyright (c) 2012 Helium End. All rights reserved.
//
#import "RBActionSheet.h"
@implementation RBActionSheet
- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles clickedBlock:(RBActionSheetWithButtonIndexBlock)clickedBlock
{
self = [super init];
if ( self )
{
[self setTitle:title];
for ( NSString * buttonTitle in otherButtonTitles )
{
[self addButtonWithTitle:buttonTitle];
}
uint index = otherButtonTitles.count;
// destructive title
if ( destructiveButtonTitle != nil )
{
[self addButtonWithTitle:destructiveButtonTitle];
self.destructiveButtonIndex = index;
index++;
}
// cancel title
if ( cancelButtonTitle != nil )
{
[self addButtonWithTitle:cancelButtonTitle];
[self setCancelButtonIndex:index];
}
self.clickedBlock = clickedBlock;
self.delegate = self;
}
return self;
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ( [super respondsToSelector:aSelector] )
{
return YES;
}
else
{
if ( aSelector == @selector(actionSheetCancel:) && self.cancelBlock != nil )
{
return YES;
}
}
return NO;
}
#pragma - Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ( self.clickedBlock != nil )
{
self.clickedBlock( self, buttonIndex );
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ( self.willDimissBlock != nil )
{
self.willDimissBlock( self, buttonIndex );
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ( self.didDismissBlock != nil )
{
self.didDismissBlock( self, buttonIndex );
}
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
if ( self.willPresetBlock != nil )
{
self.willPresetBlock( self );
}
}
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
if ( self.didPresentBlock != nil )
{
self.didPresentBlock( self );
}
}
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
// there is no checking if cancel block is nil
// because it will be handled by `respondToSelector:`
// the reason is action sheet will react differently
// if this method is defined
self.cancelBlock( self );
}
#pragma - Stactic
+ (RBActionSheet *)actionSheetWithTitle:(NSString *)title
cancelButtonTitile:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
clickedBlock:(RBActionSheetWithButtonIndexBlock)clickedBlock
{
return [[self alloc] initWithTitle:title
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:otherButtonTitles
clickedBlock:clickedBlock];
}
@end