Question

So I have UIPickerView as inputView for one of my text fields. It works perfectly: it slides up, as normal keyboard would, I can select an option and it will be populated in a text field.

The only problem is that I can directly edit the field, by typing into it. How to disallow this behavior? So that even cursor doesn't show up?

I've tried the:

- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField
{
    [self.datePick setHidden:NO];
    return NO;
}

but in this case picker stops to work completely - it doesn't appear.

Thanks for your help!

Was it helpful?

Solution

I ended up doing the following:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range     replacementString:(NSString *)string
{
    if([textField isEqual:self.birthdayF] || [textField isEqual:self.genderF])
        return NO;
    return YES;
}

so the cursor is still there, but user can't type anything in.

OTHER TIPS

You can set the textField.enabled = NO; and place a UITapGestureRecognizer on the textField and listen to the tap to display your picker.

 - (void) viewDidLoad
  {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [textField addGestureRecognizer:tap];
    [tap release];
  }


- (void) handleTap:(UITapGestureRecognizer *)tap
  {
 //do stuff
 //if you need the textField here 
   UITextField *text = (UITextField *)tap.view; 
  }

Try like this,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if([textField isEqual:selectedTextField])
         return NO;

    return YES;
}

say your textfield name is pickerTextField now in ViewDidLoad add following line

self.pickerTextField.delegate = self; (don't forget to conform UITextFieldDelegate protocol)

than implement following method

- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField
{
    if([textField isEqual:pickerTextField])
    {
    [self.datePick setHidden:NO];
    return NO;
    }
    return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top