Skip to content
Browse files

add another convenience helper, alertWithTitle

  • Loading branch information...
1 parent 9b5de5f commit f2f7ed8f87f13c9411226470a3bba40aa186991a @steipete committed Apr 25, 2011
Showing with 50 additions and 45 deletions.
  1. +1 −0 Utils/PSAlertView.h
  2. +49 −45 Utils/PSAlertView.m
View
1 Utils/PSAlertView.h
@@ -12,6 +12,7 @@
NSMutableArray *blocks_;
}
++ (PSAlertView *)alertWithTitle:(NSString *)title;
+ (PSAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message;
- (id)initWithTitle:(NSString *)title message:(NSString *)message;
View
94 Utils/PSAlertView.m
@@ -16,86 +16,90 @@ @implementation PSAlertView
#pragma mark -
#pragma mark Static
-+ (PSAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message {
- return [[[PSAlertView alloc] initWithTitle:title message:message] autorelease];
++ (PSAlertView *)alertWithTitle:(NSString *)title; {
+ return [[[PSAlertView alloc] initWithTitle:title message:nil] autorelease];
+}
+
++ (PSAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message; {
+ return [[[PSAlertView alloc] initWithTitle:title message:message] autorelease];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark NSObject
- (id)initWithTitle:(NSString *)title message:(NSString *)message {
- if ((self = [super init])) {
- view_ = [[UIAlertView alloc] initWithTitle:title
- message:message
- delegate:self
- cancelButtonTitle:nil
- otherButtonTitles:nil];
- blocks_ = [[NSMutableArray alloc] init];
- }
-
- return self;
+ if ((self = [super init])) {
+ view_ = [[UIAlertView alloc] initWithTitle:title
+ message:message
+ delegate:self
+ cancelButtonTitle:nil
+ otherButtonTitles:nil];
+ blocks_ = [[NSMutableArray alloc] init];
+ }
+
+ return self;
}
- (void)dealloc {
- view_.delegate = nil;
- MCRelease(view_);
- MCRelease(blocks_);
- [super dealloc];
+ view_.delegate = nil;
+ MCRelease(view_);
+ MCRelease(blocks_);
+ [super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public
- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block {
- assert([title length] > 0 && "cannot set empty button title");
-
- [self addButtonWithTitle:title block:block];
- view_.cancelButtonIndex = (view_.numberOfButtons - 1);
+ assert([title length] > 0 && "cannot set empty button title");
+
+ [self addButtonWithTitle:title block:block];
+ view_.cancelButtonIndex = (view_.numberOfButtons - 1);
}
- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block {
- assert([title length] > 0 && "cannot add button with empty title");
-
- if (block) {
- [blocks_ addObject:[[block copy] autorelease]];
- }
- else {
- [blocks_ addObject:[NSNull null]];
- }
-
- [view_ addButtonWithTitle:title];
+ assert([title length] > 0 && "cannot add button with empty title");
+
+ if (block) {
+ [blocks_ addObject:[[block copy] autorelease]];
+ }
+ else {
+ [blocks_ addObject:[NSNull null]];
+ }
+
+ [view_ addButtonWithTitle:title];
}
- (void)show {
- [view_ show];
-
- /* Ensure that the delegate (that's us) survives until the sheet is dismissed */
- [self retain];
+ [view_ show];
+
+ /* Ensure that the delegate (that's us) survives until the sheet is dismissed */
+ [self retain];
}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
- [view_ dismissWithClickedButtonIndex:buttonIndex animated:animated];
- [self alertView:view_ clickedButtonAtIndex:buttonIndex];
+ [view_ dismissWithClickedButtonIndex:buttonIndex animated:animated];
+ [self alertView:view_ clickedButtonAtIndex:buttonIndex];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
- /* Run the button's block */
- if (buttonIndex >= 0 && buttonIndex < [blocks_ count]) {
- id obj = [blocks_ objectAtIndex: buttonIndex];
- if (![obj isEqual:[NSNull null]]) {
- ((void (^)())obj)();
+ /* Run the button's block */
+ if (buttonIndex >= 0 && buttonIndex < [blocks_ count]) {
+ id obj = [blocks_ objectAtIndex: buttonIndex];
+ if (![obj isEqual:[NSNull null]]) {
+ ((void (^)())obj)();
+ }
}
- }
-
- /* AlertView to be dismissed, drop our self reference */
- [self release];
+
+ /* AlertView to be dismissed, drop our self reference */
+ [self release];
}
@end

0 comments on commit f2f7ed8

Please sign in to comment.
Something went wrong with that request. Please try again.