UIAlertView+LBXAlertAction.m 903 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // UIAlertView+Block.h
  3. //
  4. //
  5. // Created by lbxia on 15/10/27.
  6. // Copyright © 2015年 lbxia. All rights reserved.
  7. //
  8. #import "UIAlertView+LBXAlertAction.h"
  9. #import <objc/runtime.h>
  10. static char key;
  11. @implementation UIAlertView (LBXAlertAction)
  12. - (void(^)(NSInteger buttonIndex))block
  13. {
  14. return objc_getAssociatedObject(self, &key);;
  15. }
  16. - (void)setBlock:(void(^)(NSInteger buttonIndex))block
  17. {
  18. if (block) {
  19. objc_removeAssociatedObjects(self);
  20. objc_setAssociatedObject(self, &key, block, OBJC_ASSOCIATION_COPY);
  21. }
  22. }
  23. - (void)showWithBlock:(void(^)(NSInteger buttonIndex))block
  24. {
  25. self.block = block;
  26. self.delegate = self;
  27. [self show];
  28. }
  29. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  30. {
  31. if (self.block)
  32. {
  33. self.block(buttonIndex);
  34. }
  35. objc_removeAssociatedObjects(self);
  36. }
  37. @end