CommonEditorRangeCell.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // CommonEditorRangeCell.m
  3. // RedAnt Mobile
  4. //
  5. // Created by Jack on 2017/11/13.
  6. // Copyright © 2017年 Ray. All rights reserved.
  7. //
  8. #import "CommonEditorRangeCell.h"
  9. #import "DatePickerViewController.h"
  10. @interface CommonEditorRangeCell ()<UITextFieldDelegate>
  11. @property (strong, nonatomic) IBOutlet UILabel *titleLabel;
  12. @property (strong, nonatomic) IBOutlet UITextField *minField;
  13. @property (strong, nonatomic) IBOutlet UITextField *maxField;
  14. @end
  15. @implementation CommonEditorRangeCell
  16. - (void)awakeFromNib {
  17. [super awakeFromNib];
  18. // Initialization code
  19. }
  20. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  21. [super setSelected:selected animated:animated];
  22. // Configure the view for the selected state
  23. }
  24. #pragma mark - Setter
  25. - (void)setKeyboardType:(UIKeyboardType)keyboardType {
  26. self.minField.keyboardType = keyboardType;
  27. self.maxField.keyboardType = keyboardType;
  28. }
  29. - (void)setType:(CommonEditorRangeType)type {
  30. _type = type;
  31. }
  32. - (void)setMinValue:(NSString *)min maxValue:(NSString *)max {
  33. self.minField.text = min;
  34. self.maxField.text = max;
  35. }
  36. - (void)setTitle:(NSString *)title {
  37. self.titleLabel.text = title;
  38. }
  39. #pragma mark - TextField Delegate
  40. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  41. [textField resignFirstResponder];
  42. return YES;
  43. }
  44. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  45. return YES;
  46. }
  47. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  48. if (self.type == CommonEditorRangeTypeDate) {
  49. // 显示DatePicker
  50. [self showDatePickerWithField:textField];
  51. return NO;
  52. }
  53. return YES;
  54. }
  55. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
  56. // 判断min是否大于max
  57. if (self.type == CommonEditorRangeTypeDate) {
  58. } else {
  59. NSString *max_str = self.maxField.text;
  60. NSString *min_str = self.minField.text;
  61. float min = 0;
  62. float max = MAXFLOAT;
  63. if (textField == self.minField) {
  64. min = [min_str floatValue];
  65. max_str = [max_str stringByReplacingOccurrencesOfString:@" " withString:@""];
  66. if (max_str != nil && max_str.length > 1) {
  67. max = [max_str floatValue];
  68. }
  69. } else if (textField == self.maxField) {
  70. max = [max_str floatValue];
  71. min_str = [min_str stringByReplacingOccurrencesOfString:@" " withString:@""];
  72. if (min_str != nil && min_str.length > 1) {
  73. min = [min_str floatValue];
  74. }
  75. }
  76. if (min > max) {
  77. [self showAlert];
  78. return NO;
  79. }
  80. if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorRangeCell:didChangeMinValue:maxValue:atIndexPath:)]) {
  81. [self.delegate commonEditorRangeCell:self didChangeMinValue:self.minField.text maxValue:self.maxField.text atIndexPath:self.indexPath];
  82. }
  83. }
  84. return YES;
  85. }
  86. #pragma mark - Private
  87. - (UIViewController *)viewController {
  88. for (UIView* next = [self superview]; next; next = next.superview) {
  89. UIResponder *nextResponder = [next nextResponder];
  90. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  91. return (UIViewController *)nextResponder;
  92. }
  93. }
  94. return nil;
  95. }
  96. - (void)showAlert {
  97. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"minimum can't larger than maximum" preferredStyle:UIAlertControllerStyleAlert];
  98. UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:nil];
  99. [alert addAction:action];
  100. [[self viewController] presentViewController:alert animated:YES completion:nil];
  101. }
  102. - (void)showDatePickerWithField:(UITextField *)field {
  103. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  104. [dateFormatter setDefaultDate:[NSDate date]];
  105. [dateFormatter setDateFormat:@"MM/dd/yyyy"];
  106. DatePickerViewController* dpvc =[ [UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
  107. __weak UITextField *weakField = field;
  108. __weak typeof(self) weakSelf = self;
  109. dpvc.pickerMode = UIDatePickerModeDate;
  110. dpvc.date = [dateFormatter dateFromString:[dateFormatter stringFromDate:dateFormatter.defaultDate]];
  111. dpvc.formatter = dateFormatter;
  112. dpvc.blk_Set = ^(NSString *date) {
  113. NSString *min = @"";
  114. NSString *max = @"";
  115. // 判断min是否大于max
  116. if (weakField == weakSelf.minField) {
  117. min = date;
  118. max = [weakSelf.maxField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  119. if (max != nil && max.length > 1) {
  120. NSTimeInterval min_interval = [dateFormatter dateFromString:min].timeIntervalSince1970;
  121. NSTimeInterval max_interval = [dateFormatter dateFromString:max].timeIntervalSince1970;
  122. if (min_interval > max_interval) {
  123. [weakSelf showAlert];
  124. return ;
  125. }
  126. }
  127. } else if (weakField == weakSelf.maxField) {
  128. min = [weakSelf.minField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  129. max = date;
  130. if (min != nil && min.length > 1) {
  131. NSTimeInterval min_interval = [dateFormatter dateFromString:min].timeIntervalSince1970;
  132. NSTimeInterval max_interval = [dateFormatter dateFromString:max].timeIntervalSince1970;
  133. if (min_interval > max_interval) {
  134. [weakSelf showAlert];
  135. return ;
  136. }
  137. }
  138. }
  139. // max不小于min
  140. [weakField setText:date];
  141. if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(commonEditorRangeCell:didChangeMinValue:maxValue:atIndexPath:)]) {
  142. [weakSelf.delegate commonEditorRangeCell:weakSelf didChangeMinValue:min maxValue:max atIndexPath:weakSelf.indexPath];
  143. }
  144. };
  145. if (field == self.minField) {
  146. dpvc.title = @"Select MinDate";
  147. } else if (field == self.maxField) {
  148. dpvc.title = @"Select MaxDate";
  149. }
  150. [[self viewController].navigationController pushViewController:dpvc animated:true];
  151. }
  152. - (IBAction)resetBtnClick:(UIButton *)sender {
  153. [self setMinValue:@"" maxValue:@""];
  154. if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorRangeCell:didChangeMinValue:maxValue:atIndexPath:)]) {
  155. [self.delegate commonEditorRangeCell:self didChangeMinValue:@"" maxValue:@"" atIndexPath:self.indexPath];
  156. }
  157. }
  158. @end