| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- //
- // CommonEditorRangeCell.m
- // RedAnt Mobile
- //
- // Created by Jack on 2017/11/13.
- // Copyright © 2017年 Ray. All rights reserved.
- //
- #import "CommonEditorRangeCell.h"
- #import "DatePickerViewController.h"
- @interface CommonEditorRangeCell ()<UITextFieldDelegate>
- @property (strong, nonatomic) IBOutlet UILabel *titleLabel;
- @property (strong, nonatomic) IBOutlet UITextField *minField;
- @property (strong, nonatomic) IBOutlet UITextField *maxField;
- @end
- @implementation CommonEditorRangeCell
- - (void)awakeFromNib {
- [super awakeFromNib];
- // Initialization code
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- #pragma mark - Setter
- - (void)setKeyboardType:(UIKeyboardType)keyboardType {
- self.minField.keyboardType = keyboardType;
- self.maxField.keyboardType = keyboardType;
- }
- - (void)setType:(CommonEditorRangeType)type {
- _type = type;
- }
- - (void)setMinValue:(NSString *)min maxValue:(NSString *)max {
- self.minField.text = min;
- self.maxField.text = max;
- }
- - (void)setTitle:(NSString *)title {
- self.titleLabel.text = title;
- }
- #pragma mark - TextField Delegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- [textField resignFirstResponder];
- return YES;
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- return YES;
- }
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
- if (self.type == CommonEditorRangeTypeDate) {
- // 显示DatePicker
- [self showDatePickerWithField:textField];
- return NO;
- }
- return YES;
- }
- - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
- // 判断min是否大于max
- if (self.type == CommonEditorRangeTypeDate) {
-
- } else {
- NSString *max_str = self.maxField.text;
- NSString *min_str = self.minField.text;
- float min = 0;
- float max = MAXFLOAT;
-
- if (textField == self.minField) {
- min = [min_str floatValue];
- max_str = [max_str stringByReplacingOccurrencesOfString:@" " withString:@""];
- if (max_str != nil && max_str.length > 1) {
- max = [max_str floatValue];
- }
-
- } else if (textField == self.maxField) {
- max = [max_str floatValue];
- min_str = [min_str stringByReplacingOccurrencesOfString:@" " withString:@""];
- if (min_str != nil && min_str.length > 1) {
- min = [min_str floatValue];
- }
-
- }
-
-
- if (min > max) {
- [self showAlert];
- return NO;
- }
- if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorRangeCell:didChangeMinValue:maxValue:atIndexPath:)]) {
- [self.delegate commonEditorRangeCell:self didChangeMinValue:self.minField.text maxValue:self.maxField.text atIndexPath:self.indexPath];
- }
- }
-
- return YES;
- }
- #pragma mark - Private
- - (UIViewController *)viewController {
- for (UIView* next = [self superview]; next; next = next.superview) {
- UIResponder *nextResponder = [next nextResponder];
- if ([nextResponder isKindOfClass:[UIViewController class]]) {
- return (UIViewController *)nextResponder;
- }
- }
- return nil;
- }
- - (void)showAlert {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"minimum can't larger than maximum" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:nil];
- [alert addAction:action];
- [[self viewController] presentViewController:alert animated:YES completion:nil];
- }
- - (void)showDatePickerWithField:(UITextField *)field {
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDefaultDate:[NSDate date]];
- [dateFormatter setDateFormat:@"MM/dd/YYYY"];
-
- DatePickerViewController* dpvc =[ [UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
- __weak UITextField *weakField = field;
- __weak typeof(self) weakSelf = self;
-
- dpvc.pickerMode = UIDatePickerModeDate;
- dpvc.date = [dateFormatter dateFromString:[dateFormatter stringFromDate:dateFormatter.defaultDate]];
- dpvc.formatter = dateFormatter;
- dpvc.blk_Set = ^(NSString *date) {
- NSString *min = @"";
- NSString *max = @"";
- // 判断min是否大于max
- if (weakField == weakSelf.minField) {
- min = date;
- max = [weakSelf.maxField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
- if (max != nil && max.length > 1) {
-
- NSTimeInterval min_interval = [dateFormatter dateFromString:min].timeIntervalSince1970;
- NSTimeInterval max_interval = [dateFormatter dateFromString:max].timeIntervalSince1970;
- if (min_interval > max_interval) {
- [weakSelf showAlert];
- return ;
- }
- }
-
- } else if (weakField == weakSelf.maxField) {
- min = [weakSelf.minField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
- max = date;
- if (min != nil && min.length > 1) {
- NSTimeInterval min_interval = [dateFormatter dateFromString:min].timeIntervalSince1970;
- NSTimeInterval max_interval = [dateFormatter dateFromString:max].timeIntervalSince1970;
- if (min_interval > max_interval) {
- [weakSelf showAlert];
- return ;
- }
- }
- }
-
- // max不小于min
- [weakField setText:date];
- if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(commonEditorRangeCell:didChangeMinValue:maxValue:atIndexPath:)]) {
- [weakSelf.delegate commonEditorRangeCell:weakSelf didChangeMinValue:min maxValue:max atIndexPath:weakSelf.indexPath];
- }
- };
- if (field == self.minField) {
- dpvc.title = @"Select MinDate";
- } else if (field == self.maxField) {
- dpvc.title = @"Select MaxDate";
- }
-
- [[self viewController].navigationController pushViewController:dpvc animated:true];
- }
- - (IBAction)resetBtnClick:(UIButton *)sender {
-
- [self setMinValue:@"" maxValue:@""];
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(commonEditorRangeCell:didChangeMinValue:maxValue:atIndexPath:)]) {
- [self.delegate commonEditorRangeCell:self didChangeMinValue:@"" maxValue:@"" atIndexPath:self.indexPath];
- }
- }
- @end
|