-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRPAlertView.m
106 lines (91 loc) · 3.44 KB
/
PRPAlertView.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
/***
* Excerpted from "iOS Recipes",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/cdirec for more book information.
***/
//
// PRPAlertView.m
// PRPAlertView
//
// Created by Matt Drance on 1/24/11.
// Copyright 2011 Bookhouse Software LLC. All rights reserved.
//
#import "PRPAlertView.h"
@interface PRPAlertView ()
@property (nonatomic, copy) PRPAlertBlock cancelBlock;
@property (nonatomic, copy) PRPAlertBlock otherBlock;
@property (nonatomic, copy) NSString *cancelButtonTitle;
@property (nonatomic, copy) NSString *otherButtonTitle;
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
cancelTitle:(NSString *)cancelTitle
cancelBlock:(PRPAlertBlock)cancelBlock
otherTitle:(NSString *)otherTitle
otherBlock:(PRPAlertBlock)otherBlock;
@end
@implementation PRPAlertView
@synthesize cancelBlock;
@synthesize otherBlock;
@synthesize cancelButtonTitle;
@synthesize otherButtonTitle;
+ (void)showWithTitle:(NSString *)title
message:(NSString *)message
buttonTitle:(NSString *)buttonTitle {
[self showWithTitle:title message:message
cancelTitle:buttonTitle cancelBlock:nil
otherTitle:nil otherBlock:nil];
}
+ (void)showWithTitle:(NSString *)title
message:(NSString *)message
cancelTitle:(NSString *)cancelTitle
cancelBlock:(PRPAlertBlock)cancelBlk
otherTitle:(NSString *)otherTitle
otherBlock:(PRPAlertBlock)otherBlk {
[[[[self alloc] initWithTitle:title message:message
cancelTitle:cancelTitle cancelBlock:cancelBlk
otherTitle:otherTitle otherBlock:otherBlk]
autorelease] show];
}
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
cancelTitle:(NSString *)cancelTitle
cancelBlock:(PRPAlertBlock)cancelBlk
otherTitle:(NSString *)otherTitle
otherBlock:(PRPAlertBlock)otherBlk {
if ((self = [super initWithTitle:title
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:otherTitle, nil])) {
if (cancelBlk == nil && otherBlk == nil) {
self.delegate = nil;
}
self.cancelButtonTitle = cancelTitle;
self.otherButtonTitle = otherTitle;
self.cancelBlock = cancelBlk;
self.otherBlock = otherBlk;
}
return self;
}
#pragma mark -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView
willDismissWithButtonIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:self.cancelButtonTitle]) {
if (self.cancelBlock) self.cancelBlock();
} else if ([buttonTitle isEqualToString:self.otherButtonTitle]) {
if (self.otherBlock) self.otherBlock();
}
}
- (void)dealloc {
[cancelButtonTitle release], cancelButtonTitle = nil;
[otherButtonTitle release], otherButtonTitle = nil;
[cancelBlock release], cancelBlock = nil;
[otherBlock release], otherBlock = nil;
[super dealloc];
}
@end