Header file
//// UIAlertView + Utils. h // UIView + Categories /// Created by su xinde on 13-4-11. // Copyright (c) 2013 su xinde. all rights reserved. // # import
/** Completion handler invoked when user taps a button. ** @ param alertView The alert view being shown. * @ param buttonIndex The index of the button tapped. */typedef void (^ UIAlertViewHandler) (UIAlertView * alertView, NSInteger buttonIndex);/*** Category of 'uialertview' that offers a completion handler to listen to interaction. this avoids the need of the implementation of the delegate pattern. ** @ warning Completion handler: Invoked when user taps a button. ** typedef void (^ UIAlertViewHandler) (UIAlertView * alertView, NSInteger buttonIndex); **-* alertView * The alert view being shown. *-* buttonIndex * The index of the button tapped. */@ interface UIAlertView (Utils)/*** Shows the handler er alert with the given handler. ** @ param handler The handler that will be invoked in user interaction. */-(void) showWithHandler :( UIAlertViewHandler) handler;/*** Utility selector to show an alert with a title, a message and a button to dimiss. ** @ param title The title of the alert. * @ param message The message to show in the alert. * @ param handler The handler that will be invoked in user interaction. */+ (void) showWithTitle :( NSString *) title message :( NSString *) message handler :( UIAlertViewHandler) handler; /*** Utility selector to show an alert with an "Error" title, a message and a button to dimiss. ** @ param message The message to show in the alert. * @ param handler The handler that will be invoked in user interaction. */+ (void) showErrorWithMessage :( NSString *) message handler :( UIAlertViewHandler) handler;/*** Utility selector to show an alert with a "Warning" title, a message and a button to dimiss. ** @ param message The message to show in the alert. * @ param handler The handler that will be invoked in user interaction. */+ (void) showWarningWithMessage :( NSString *) message handler :( UIAlertViewHandler) handler;/*** Utility selector to show a confirmation dialog with a title, a message and two buttons to accept or cancel. ** @ param title The title of the alert. * @ param message The message to show in the alert. * @ param handler The handler that will be invoked in user interaction. */+ (void) showConfirmationDialogWithTitle :( NSString *) title message :( NSString *) message handler :( UIAlertViewHandler) handler; @ end
Implementation File
//// UIAlertView + Utils. m // UIView + Categories /// Created by su xinde on 13-4-11. // Copyright (c) 2013 su xinde. all rights reserved. // # import "UIAlertView + Utils. h "# import
/** Runtime association key. */static NSString * kHandlerAssociatedKey = @ "kHandlerAssociatedKey"; @ implementation UIAlertView (Utils) # pragma mark-Showing/** Shows the handler er alert with the given handler. */-(void) showWithHandler :( UIAlertViewHandler) handler {objc_setAssociatedObject (self, (const void *) (kHandlerAssociatedKey), handler, listener); [self setDelegate: sel F]; [self show] ;}# pragma mark-UIAlertViewDelegate/** Sent to the delegate when the user clicks a button on an alert view. */-(void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {complete completionHandler = aggregate (self, (const void *) (complete); if (completionHandler! = Nil) {completionHandler (alertView, buttonIndex) ;}# pragma mark-Utility methods/** Utility selector to show an alert with a title, a message and a button to dimiss. */+ (void) showWithTitle :( NSString *) title message :( NSString *) message handler :( UIAlertViewHandler) handler {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: title message: message delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert showWithHandler: handler];}/** Utility selector to show an alert with an "Error" title, a message and a button to dimiss. */+ (void) showErrorWithMessage :( NSString *) message handler :( UIAlertViewHandler) handler {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Error" message: message delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert showWithHandler: handler];}/** Utility selector to show an alert with a "Warning" title, a message and a button to dimiss. */+ (void) showWarningWithMessage :( NSString *) message handler :( handle) handler {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Warning" message: message delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert showWithHandler: handler];}/** Utility selector to show a confirmation dialog with a title, a message and two buttons to accept or cancel. */+ (void) handle :( NSString *) title message :( NSString *) message handler :( UIAlertViewHandler) handler {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: title message: message delegate: nil cancelButtonTitle: @ "No" otherButtonTitles: @ "Yes", nil]; [alert showWithHandler: handler];} @ end