How to remove setMaximumDate for UIDatePicker in ios

UseriOSDev Source

I am using one UIDatePicker for two UITextfields in the same page.For one UITextfield I am setting the setMaximumDate and for the other UITextfield I don't want the maximum date.But still the other UITextfield is as well fetching the same setMaximumDate that is set for the first textfield. Is there any way to remove the already set setMaximumDate for the second textfield in UIDatePicker?

iosobjective-cswiftuitextfielduidatepicker

Answers

answered 3 months ago Krunal #1

Handle max date assignment in 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
}

comments powered by Disqus