-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBAlertView.m
122 lines (104 loc) · 3.56 KB
/
RBAlertView.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
//
// RBAlertView.m
//
#import "RBAlertView.h"
@implementation RBAlertView
- (id)initWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles clickedButtonAtIndexBlock:(RBAlertViewWithButtonIndexBlock)clickedBlock
{
self = [super initWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
if ( self ) {
for (NSString * title in otherButtonTitles) {
[self addButtonWithTitle:title];
}
self.clickedBlock = clickedBlock;
self.delegate = self;
}
return self;
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ( [super respondsToSelector:aSelector] )
{
return YES;
}
else
{
if ( aSelector == @selector(alertViewCancel:) && self.cancelBlock != nil )
{
return YES;
}
else if ( aSelector == @selector(alertViewShouldEnableFirstOtherButton:) && self.shouldEnableFirstOtherButtonBlock != nil )
{
return YES;
}
}
return NO;
}
#pragma - Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ( self.clickedBlock != nil )
{
self.clickedBlock( self, buttonIndex );
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ( self.willDismissBlock != nil )
{
self.willDismissBlock( self, buttonIndex );
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ( self.didDismissBlock != nil )
{
self.didDismissBlock( self, buttonIndex );
}
}
- (void)willPresentAlertView:(UIAlertView *)alertView
{
if ( self.willPresentBlock != nil )
{
self.willPresentBlock( self );
}
}
- (void)didPresentAlertView:(UIAlertView *)alertView
{
if ( self.didPresentBlock != nil )
{
self.didPresentBlock( self );
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
// there is no checking if shouldEnableFirstOtheButtonBlock is nil
// because it will be handled by `respondToSelector:`
// the reason is to make life easier
return self.shouldEnableFirstOtherButtonBlock( self );
}
- (void)alertViewCancel:(UIAlertView *)alertView
{
// 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 - Static
+ (RBAlertView *)alertViewWithTitle:(NSString *)title message:(NSString *)message
{
return [[self alloc] initWithTitle:title message:message cancelButtonTitle:@"OK" otherButtonTitles:nil clickedButtonAtIndexBlock:nil];
}
+ (RBAlertView *)alertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle
{
return [[self alloc] initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil clickedButtonAtIndexBlock:nil];
}
+ (RBAlertView *)alertViewWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles
clickedButtonAtIndexBlock:(RBAlertViewWithButtonIndexBlock)clickedBlock
{
return [[self alloc] initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles clickedButtonAtIndexBlock:clickedBlock];
}
@end