LBXAlertAction.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // LBXAlertAction.m
  3. //
  4. // https://github.com/MxABC/LBXAlertAction
  5. // Created by lbxia on 15/10/27.
  6. // Copyright © 2015年 lbxia. All rights reserved.
  7. //
  8. #import "LBXAlertAction.h"
  9. #import <UIKit/UIKit.h>
  10. #import "UIAlertView+LBXAlertAction.h"
  11. #import "UIActionSheet+LBXAlertAction.h"
  12. #import "UIWindow+LBXHierarchy.h"
  13. @implementation LBXAlertAction
  14. + (BOOL)isIosVersion8AndAfter
  15. {
  16. return [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ;
  17. }
  18. + (void)showAlertWithTitle:(NSString*)title msg:(NSString*)message buttonsStatement:(NSArray<NSString*>*)arrayItems chooseBlock:(void (^)(NSInteger buttonIdx))block
  19. {
  20. NSMutableArray* argsArray = [[NSMutableArray alloc] initWithArray:arrayItems];
  21. if ( [LBXAlertAction isIosVersion8AndAfter])
  22. {
  23. //UIAlertController style
  24. UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  25. for (int i = 0; i < [argsArray count]; i++)
  26. {
  27. UIAlertActionStyle style = (0 == i)? UIAlertActionStyleCancel: UIAlertActionStyleDefault;
  28. // Create the actions.
  29. UIAlertAction *action = [UIAlertAction actionWithTitle:[argsArray objectAtIndex:i] style:style handler:^(UIAlertAction *action) {
  30. if (block) {
  31. block(i);
  32. }
  33. }];
  34. [alertController addAction:action];
  35. }
  36. [[LBXAlertAction getTopViewController] presentViewController:alertController animated:YES completion:nil];
  37. return;
  38. }
  39. //UIAlertView style
  40. UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:argsArray[0] otherButtonTitles:nil, nil];
  41. [argsArray removeObject:argsArray[0]];
  42. for (NSString *buttonTitle in argsArray) {
  43. NSLog(@"buttonTitle:%@",buttonTitle);
  44. [alertView addButtonWithTitle:buttonTitle];
  45. }
  46. [alertView showWithBlock:^(NSInteger buttonIdx)
  47. {
  48. block(buttonIdx);
  49. }];
  50. }
  51. + (UIViewController*)getTopViewController
  52. {
  53. UIWindow * window = [[UIApplication sharedApplication] keyWindow];
  54. return window.currentViewController;
  55. }
  56. + (void)showActionSheetWithTitle:(NSString*)title
  57. message:(NSString*)message
  58. cancelButtonTitle:(NSString*)cancelString
  59. destructiveButtonTitle:(NSString*)destructiveButtonTitle
  60. otherButtonTitle:(NSArray<NSString*>*)otherButtonArray
  61. chooseBlock:(void (^)(NSInteger buttonIdx))block
  62. {
  63. NSMutableArray* argsArray = [[NSMutableArray alloc] initWithCapacity:3];
  64. if (cancelString) {
  65. [argsArray addObject:cancelString];
  66. }
  67. if (destructiveButtonTitle) {
  68. [argsArray addObject:destructiveButtonTitle];
  69. }
  70. [argsArray addObjectsFromArray:otherButtonArray];
  71. if ( [LBXAlertAction isIosVersion8AndAfter])
  72. {
  73. UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
  74. for (int i = 0; i < [argsArray count]; i++)
  75. {
  76. UIAlertActionStyle style = (0 == i)? UIAlertActionStyleCancel: UIAlertActionStyleDefault;
  77. if (1==i && destructiveButtonTitle) {
  78. style = UIAlertActionStyleDestructive;
  79. }
  80. // Create the actions.
  81. UIAlertAction *action = [UIAlertAction actionWithTitle:[argsArray objectAtIndex:i] style:style handler:^(UIAlertAction *action) {
  82. if (block) {
  83. block(i);
  84. }
  85. }];
  86. [alertController addAction:action];
  87. }
  88. [[LBXAlertAction getTopViewController] presentViewController:alertController animated:YES completion:nil];
  89. return;
  90. }
  91. //UIActionSheet
  92. UIView *view = [self getTopViewController].view;
  93. UIActionSheet *sheet = nil;
  94. NSInteger count = argsArray.count;
  95. if (cancelString) {
  96. [argsArray removeObject:cancelString];
  97. }
  98. if (destructiveButtonTitle) {
  99. [argsArray removeObject:destructiveButtonTitle];
  100. }
  101. if (argsArray.count == 0)
  102. {
  103. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:nil, nil];
  104. }
  105. switch (argsArray.count) {
  106. case 0:
  107. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:nil, nil];
  108. break;
  109. case 1:
  110. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:argsArray[0], nil];
  111. break;
  112. case 2:
  113. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:argsArray[0],argsArray[1], nil];
  114. break;
  115. case 3:
  116. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:argsArray[0],argsArray[1],argsArray[2], nil];
  117. break;
  118. case 4:
  119. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:argsArray[0],argsArray[1],argsArray[2],argsArray[3], nil];
  120. break;
  121. case 5:
  122. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:argsArray[0],argsArray[1],argsArray[2],argsArray[3],argsArray[4], nil];
  123. break;
  124. case 6:
  125. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:argsArray[0],argsArray[1],argsArray[2],argsArray[3],argsArray[4],argsArray[5], nil];
  126. break;
  127. case 7:
  128. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:argsArray[0],argsArray[1],argsArray[2],argsArray[3],argsArray[4],argsArray[5],argsArray[6], nil];
  129. break;
  130. case 8:
  131. sheet = [[UIActionSheet alloc]initWithTitle:title delegate:nil cancelButtonTitle:cancelString destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:argsArray[0],argsArray[1],argsArray[2],argsArray[3],argsArray[4],argsArray[5],argsArray[6],argsArray[7], nil];
  132. break;
  133. default:
  134. break;
  135. }
  136. [sheet showInView:view block:^(NSInteger buttonIdx,NSString* buttonTitle)
  137. {
  138. NSInteger idx = buttonIdx;
  139. if (idx == count -1) {
  140. idx = 0;
  141. }
  142. else
  143. {
  144. ++idx;
  145. }
  146. // NSLog(@"idx:%ld",idx);
  147. if (block) {
  148. block(idx);
  149. }
  150. }];
  151. }
  152. @end