From a1e5adf606cf0b2e760f473a992b1b24bda9d6bb Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Wed, 14 Nov 2012 22:08:32 -0800 Subject: [PATCH] add an alert view shortcut that accepts OK & cancel button titles --- Marshmallows/UIAlertView+marshmallows.h | 11 ++++++++--- Marshmallows/UIAlertView+marshmallows.m | 15 ++++++++++++--- Marshmallows/UIAlertViewDelegate.h | 4 +++- Marshmallows/UIAlertViewDelegate.m | 4 ++-- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Marshmallows/UIAlertView+marshmallows.h b/Marshmallows/UIAlertView+marshmallows.h index b24b882..63e8afc 100644 --- a/Marshmallows/UIAlertView+marshmallows.h +++ b/Marshmallows/UIAlertView+marshmallows.h @@ -3,15 +3,20 @@ // DatingX // // Created by Sami Samhuri on 11-08-24. -// Copyright 2011 __MyCompanyName__. All rights reserved. +// Copyright 2011 Guru Logic. All rights reserved. // #import - -typedef void (^UIAlertViewCallback)(BOOL ok); +#import "UIAlertViewDelegate.h" @interface UIAlertView (UIAlertView_marshmallows) + (void) showAlertWithTitle: (NSString *)title message: (NSString *)message; ++ (void) confirmWithTitle: (NSString *)title message: (NSString *)message then: (UIAlertViewCallback)callback; ++ (void) confirmWithTitle: (NSString *)title + message: (NSString *)message + cancelTitle: (NSString *)cancelTitle + okTitle: (NSString *)okTitle + then: (UIAlertViewCallback)callback; @end diff --git a/Marshmallows/UIAlertView+marshmallows.m b/Marshmallows/UIAlertView+marshmallows.m index 36d3e3d..abe7aa3 100644 --- a/Marshmallows/UIAlertView+marshmallows.m +++ b/Marshmallows/UIAlertView+marshmallows.m @@ -3,7 +3,7 @@ // DatingX // // Created by Sami Samhuri on 11-08-24. -// Copyright 2011 __MyCompanyName__. All rights reserved. +// Copyright 2011 Guru Logic. All rights reserved. // #import "UIAlertView+marshmallows.h" @@ -21,12 +21,21 @@ } + (void) confirmWithTitle: (NSString *)title message: (NSString *)message then: (UIAlertViewCallback)callback +{ + [self confirmWithTitle: title message: message cancelTitle: @"Cancel" okTitle: @"OK" then: callback]; +} + ++ (void) confirmWithTitle: (NSString *)title + message: (NSString *)message + cancelTitle: (NSString *)cancelTitle + okTitle: (NSString *)okTitle + then: (UIAlertViewCallback)callback { [[[[self alloc] initWithTitle: title message: message delegate: [UIAlertViewDelegate alertViewDelegateWithCallback: callback] - cancelButtonTitle: @"Cancel" - otherButtonTitles: @"OK", nil] autorelease] show]; + cancelButtonTitle: cancelTitle + otherButtonTitles: okTitle, nil] autorelease] show]; } @end diff --git a/Marshmallows/UIAlertViewDelegate.h b/Marshmallows/UIAlertViewDelegate.h index 997526a..3c8620c 100644 --- a/Marshmallows/UIAlertViewDelegate.h +++ b/Marshmallows/UIAlertViewDelegate.h @@ -7,7 +7,9 @@ // #import -#import "UIAlertView+marshmallows.h" +#import + +typedef void (^UIAlertViewCallback)(int buttonClicked, BOOL canceled); @interface UIAlertViewDelegate : NSObject { diff --git a/Marshmallows/UIAlertViewDelegate.m b/Marshmallows/UIAlertViewDelegate.m index 4c9f19d..3b6c71a 100644 --- a/Marshmallows/UIAlertViewDelegate.m +++ b/Marshmallows/UIAlertViewDelegate.m @@ -26,8 +26,8 @@ - (void) alertView: (UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex { - BOOL ok = (buttonIndex == 1); - _callback(ok); + BOOL canceled = (buttonIndex == 0); + _callback(buttonIndex, canceled); } - (void) dealloc