How to remove setMaximumDate for UIDatePicker in ios

I use one UIDatePicker for two UIText fields on the same page. For one UIText field I set setMaximumDate, and for another UITextfield I do not need the maximum date. But still, another UIText field also retrieves the same setMaximumDate that is set for the first text field. Is there a way to remove the already setMaximumDate for the second text field in the UIDatePicker?

+4
source share
1 answer

Assign maximum date assignment to UITextFieldDelegate.

Objective-c

UIDatePicker * datePicker;
@property(nonatomic, strong) UITextField * textField1;
@property(nonatomic, strong) UITextField * textField2;


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    if (textField == self.textField1) {
        datePicker.maximumDate = [NSDate date];
    } else if (textField == self.textField2) {
        datePicker.maximumDate = nil;
    }

    return true;
}

Swift

let datePicker = UIDatePicker()

let textField1 =  UITextField()
let textField2 = UITextField()

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

    if textField == textField1 {
        datePicker.maximumDate = Date()
    } else if textField == textField2 {
        datePicker.maximumDate = nil
    }

    return true
}
+2
source

Source: https://habr.com/ru/post/1694820/


All Articles