Created
July 21, 2011 13:58
-
-
Save hiddenmemory/1097241 to your computer and use it in GitHub Desktop.
The implementation for a block based UIAlertView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)deleteFile:(NSString*)file { | |
[[HMAlertView alertViewWithTitle:@"Delete File" | |
message:@"Are you sure?" | |
cancelTitle:NSLocalizedString(@"No", @"") | |
cancelAction:nil | |
confirmTitle:NSLocalizedString(@"Yes", @"") | |
confirmAction:^(void) { | |
[[NSFileManager defaultManager] removeItemAtPath:file error:nil]; | |
}] show]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HMAlertView.h | |
// Created by Chris Ross on 21/07/2011. | |
// | |
#import <UIKit/UIKit.h> | |
@interface HMAlertView : UIAlertView | |
+ (HMAlertView*)alertViewWithTitle:(NSString*)title | |
message:(NSString*)message | |
cancelTitle:(NSString*)cancelTitle | |
cancelAction:(void(^)())cancelAction | |
confirmTitle:(NSString*)confirmTitle | |
confirmAction:(void(^)())confirmAction; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HMAlertView.m | |
// Created by Chris Ross on 21/07/2011. | |
// | |
#import "HMAlertView.h" | |
@interface HMHiddenAlertViewDelegate : NSObject { | |
void(^confirmAction)(); | |
void(^cancelAction)(); | |
} | |
- (id)initWithCancelAction:(void(^)())cancelAction confirmAction:(void(^)())confirmAction; | |
@end; | |
@implementation HMHiddenAlertViewDelegate | |
- (id)initWithCancelAction:(void(^)())_cancelAction confirmAction:(void(^)())_confirmAction { | |
self = [super init]; | |
if( self ) { | |
cancelAction = [_cancelAction copy]; | |
confirmAction = [_confirmAction copy]; | |
} | |
return self; | |
} | |
- (void)dealloc { | |
[cancelAction release]; | |
[confirmAction release]; | |
[super dealloc]; | |
} | |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { | |
if( buttonIndex == 0 && cancelAction ) { | |
cancelAction(); | |
} | |
if( buttonIndex == 1 && confirmAction ) { | |
confirmAction(); | |
} | |
[self autorelease]; | |
} | |
@end | |
@implementation HMAlertView | |
+ (HMAlertView*)alertViewWithTitle:(NSString*)title | |
message:(NSString*)message | |
cancelTitle:(NSString*)cancelTitle | |
cancelAction:(void(^)())cancelAction | |
confirmTitle:(NSString*)confirmTitle | |
confirmAction:(void(^)())confirmAction { | |
return [[[UIAlertView alloc] initWithTitle:title | |
message:message | |
delegate:[[HMHiddenAlertViewDelegate alloc] initWithCancelAction:cancelAction confirmAction:confirmAction] | |
cancelButtonTitle:cancelTitle | |
otherButtonTitles:confirmTitle, nil] autorelease]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment