Question

I have view controller that contains three UITextFields called "Name", "Location" and "Birthday".

I have this code here which opens a UIDatePicker:

// Open date picker
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    // Make a new view, or do what you want here
    UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,245,0,0)];
    [self.view addSubview:pv];
    return NO;
}

Here I have two problems.

Problem 1

When I tap any field the datepicker opens. I just want to open the datepicker when "Birthday" is tapped.

Problem 2

When I tap on a field, then tap again, it opens another datepicker.

How can I stop this from happening?

Peter

Was it helpful?

Solution

Instead try this on viewDidLoad:

UIDatePicker *datePicker = [[UIDatePicker alloc] init];
birtdayTextField.inputView = datePicker; //birtdayTextField is your "birthday text field"

OTHER TIPS

For opening the datepicker only for a specific textField, check the (UITextField *)textField before opening datePicker. You can set tags to each textField and then use that tag to differentiate.

For your second issue, you need to use resignFirstResponder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top