UIAlertView replaces delegate with block

Source: Internet
Author: User

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
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.