Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 106 lines (81 sloc) 3.145 kb
8a6043f Peter Steinberger initial commit
authored
1 //
2 // PSAlertView.m
3 //
4 // Created by Peter Steinberger on 17.03.10.
5 // Loosely based on Landon Fullers "Using Blocks", Plausible Labs Cooperative.
6 // http://landonf.bikemonkey.org/code/iphone/Using_Blocks_1.20090704.html
7 //
8
9 #import "PSAlertView.h"
10
11 @implementation PSAlertView
12
dc52e4c Peter Steinberger add missing delegate call to PSAlertView
authored
13 @synthesize alertView = view_;
7436932 Peter Steinberger made UIAlertView in PSAlertView accessible. You need that for some uses ...
authored
14
8a6043f Peter Steinberger initial commit
authored
15 ///////////////////////////////////////////////////////////////////////////////////////////////////
16 #pragma mark -
17 #pragma mark Static
18
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
19 + (PSAlertView *)alertWithTitle:(NSString *)title; {
20 return [[[PSAlertView alloc] initWithTitle:title message:nil] autorelease];
21 }
22
23 + (PSAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message; {
24 return [[[PSAlertView alloc] initWithTitle:title message:message] autorelease];
8a6043f Peter Steinberger initial commit
authored
25 }
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////
28 #pragma mark -
29 #pragma mark NSObject
30
31 - (id)initWithTitle:(NSString *)title message:(NSString *)message {
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
32 if ((self = [super init])) {
33 view_ = [[UIAlertView alloc] initWithTitle:title
34 message:message
35 delegate:self
36 cancelButtonTitle:nil
37 otherButtonTitles:nil];
38 blocks_ = [[NSMutableArray alloc] init];
39 }
40
41 return self;
8a6043f Peter Steinberger initial commit
authored
42 }
43
44 - (void)dealloc {
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
45 view_.delegate = nil;
46 MCRelease(view_);
47 MCRelease(blocks_);
48 [super dealloc];
8a6043f Peter Steinberger initial commit
authored
49 }
50
51 ///////////////////////////////////////////////////////////////////////////////////////////////////
52 #pragma mark -
53 #pragma mark Public
54
55 - (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block {
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
56 assert([title length] > 0 && "cannot set empty button title");
57
58 [self addButtonWithTitle:title block:block];
59 view_.cancelButtonIndex = (view_.numberOfButtons - 1);
8a6043f Peter Steinberger initial commit
authored
60 }
61
62 - (void)addButtonWithTitle:(NSString *)title block:(void (^)())block {
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
63 assert([title length] > 0 && "cannot add button with empty title");
64
65 if (block) {
66 [blocks_ addObject:[[block copy] autorelease]];
67 }
68 else {
69 [blocks_ addObject:[NSNull null]];
70 }
71
72 [view_ addButtonWithTitle:title];
8a6043f Peter Steinberger initial commit
authored
73 }
74
75 - (void)show {
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
76 [view_ show];
77
78 /* Ensure that the delegate (that's us) survives until the sheet is dismissed */
79 [self retain];
8a6043f Peter Steinberger initial commit
authored
80 }
81
82
83 - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
84 [view_ dismissWithClickedButtonIndex:buttonIndex animated:animated];
85 [self alertView:view_ clickedButtonAtIndex:buttonIndex];
8a6043f Peter Steinberger initial commit
authored
86 }
87
88 ///////////////////////////////////////////////////////////////////////////////////////////////////
89 #pragma mark -
90 #pragma mark UIAlertViewDelegate
91
92 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
93 /* Run the button's block */
94 if (buttonIndex >= 0 && buttonIndex < [blocks_ count]) {
95 id obj = [blocks_ objectAtIndex: buttonIndex];
96 if (![obj isEqual:[NSNull null]]) {
97 ((void (^)())obj)();
98 }
8a6043f Peter Steinberger initial commit
authored
99 }
f2f7ed8 Peter Steinberger add another convenience helper, alertWithTitle
authored
100
101 /* AlertView to be dismissed, drop our self reference */
102 [self release];
8a6043f Peter Steinberger initial commit
authored
103 }
104
105 @end
Something went wrong with that request. Please try again.