RQAlertController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // RQAlertController.m
  3. // LN_School
  4. //
  5. // Created by 张嵘 on 2018/10/12.
  6. // Copyright © 2018 Danson. All rights reserved.
  7. //
  8. #import "RQAlertController.h"
  9. @interface RQAlertController ()
  10. @end
  11. @implementation RQAlertController
  12. + (void)showAlertAtViewController:(nonnull UIViewController *)viewController
  13. WithTitle:(nullable NSString *)title
  14. message:(nullable NSString *)message
  15. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  16. cancelButtonTitle:(nonnull NSString *)cancelButtonTitle
  17. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  18. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  19. preferredActionTitle:(nullable NSString *)preferredActionTitle
  20. completion:(nullable RQAlertViewCompletion)completion {
  21. __block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertControllerStyle];
  22. void (^alertActionHandler) (UIAlertAction *) = [^(UIAlertAction *action) {
  23. if (completion) {
  24. if (action.style == UIAlertActionStyleCancel) {
  25. completion(NSNotFound);
  26. }else {
  27. NSUInteger index = [alertController.actions indexOfObject:action];
  28. completion(index - 1);
  29. }
  30. }
  31. alertController = nil;
  32. } copy];
  33. [alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:alertActionHandler]];
  34. @synchronized (alertController) {
  35. for (NSString *buttonTitle in otherButtonTitles) {
  36. NSNumber *actionStyleNumber = [otherButtonStyles valueForKey:buttonTitle];
  37. UIAlertActionStyle actionStyle = UIAlertActionStyleDefault;
  38. if (actionStyleNumber) {
  39. actionStyle = [actionStyleNumber integerValue];
  40. }
  41. UIAlertAction *action = [UIAlertAction actionWithTitle:buttonTitle
  42. style:actionStyle
  43. handler:alertActionHandler];
  44. [alertController addAction:action];
  45. ///Support for iOS9 add preferredAction for highlights the text of that action
  46. if ([alertController respondsToSelector:@selector(setPreferredAction:)]) {
  47. if ([preferredActionTitle isEqualToString:buttonTitle]) {
  48. if (@available(iOS 9.0, *)) {
  49. [alertController setPreferredAction:action];
  50. } else {
  51. // Fallback on earlier versions
  52. }
  53. }
  54. }
  55. }
  56. }
  57. [[RQ_SHARE_FUNCTION getCurrentVC] presentViewController:alertController animated:YES completion:nil];
  58. }
  59. + (void)showAlertWithTitle:(nullable NSString *)title
  60. message:(nullable NSString *)message
  61. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  62. cancelButtonTitle:(nonnull NSString *)cancelButtonTitle
  63. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  64. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  65. preferredActionTitle:(nullable NSString *)preferredActionTitle
  66. completion:(nullable RQAlertViewCompletion)completion{
  67. [self showAlertAtViewController:[RQ_SHARE_FUNCTION getCurrentVC]
  68. WithTitle:title
  69. message:message
  70. alertControllerStyle:alertControllerStyle
  71. cancelButtonTitle:cancelButtonTitle
  72. otherButtonTitles:otherButtonTitles
  73. otherButtonStyles:otherButtonStyles
  74. preferredActionTitle:preferredActionTitle
  75. completion:completion];
  76. }
  77. + (void)showAlertWithTitle:(nullable NSString *)title
  78. message:(nullable NSString *)message
  79. alertControllerStyle:(UIAlertControllerStyle)alertControllerStyle
  80. cancelButtonTitle:(nonnull NSString *)cancelButtonTitle
  81. otherButtonTitles:(nullable NSArray *)otherButtonTitles
  82. otherButtonStyles:(nullable NSDictionary *)otherButtonStyles
  83. completion:(nullable RQAlertViewCompletion)completion {
  84. [self showAlertAtViewController:[RQ_SHARE_FUNCTION getCurrentVC]
  85. WithTitle:title
  86. message:message
  87. alertControllerStyle:alertControllerStyle
  88. cancelButtonTitle:cancelButtonTitle
  89. otherButtonTitles:otherButtonTitles
  90. otherButtonStyles:otherButtonStyles
  91. preferredActionTitle:nil
  92. completion:completion];
  93. }
  94. @end