user3202533
user3202533

Reputation: 23

UITextField with UIPickerView keyboard bug

I just want to ask if how can I prevent keyboard popping out when it's pressing the UITextField? I have a UITextField and UIPickerView if I press the UITextField for the first time it's fine, it's showing the UIPickerView but then after i select in the UIPickerView then press the textfield again, instead of showing the UIPickerView again it shows the keyboard? I have this method on my UITextField when you click it it shows the UIPickerView:

- (IBAction)clickText:(id)sender
{
    int tag = [(UITextField*)sender tag];
    self.myPicker.hidden = NO;
    selectedTable = tag;
    [sender resignFirstResponder];
    float yy = 10;
    switch (tag) {
        case 0: yy = self.txtLeasename.frame.origin.y + self.myPicker.frame.size.height;
            break;
        case 1: yy = self.txtFeet.frame.origin.y + self.myPicker.frame.size.height;
            break;
        case 2: yy = self.txtInches.frame.origin.y + self.myPicker.frame.size.height;
            break;
        default:
            break;
    }
}

How can i fix this kind of bug? Thank you very much!

Upvotes: 0

Views: 513

Answers (2)

Desdenova
Desdenova

Reputation: 5378

Are you looking for this?

UITextField Class Reference

inputView
The custom input view to display when the text field becomes the first responder.

@property(readwrite, retain) UIView *inputView
Discussion
If the value in this property is nil, the text field displays the standard system keyboard when it becomes first responder. Assigning a custom view to this property causes that view to be presented instead.

The default value of this property is nil.

Availability
Available in iOS 3.2 and later.
Declared In
UITextField.h

So just set the proper inputView for your UITextField in viewDidLoad or else where. Something like:

self.myTextField.inputView = self.myPicker

Upvotes: 0

matthisb
matthisb

Reputation: 1118

Implement this Method and don't forget to assign the textfield.delegate to your controller

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // Check if the given textfield is your textfield with the date picker.
    if (textField.tag == 99) {
       // Then add your picker to your view
       UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMakeZero];
       [self.view addSubview:pv]; 

       // And return NO
       return NO; // Return NO prevents your Textfield from showing the Keyboard
    }

    return YES;

}

This should work for you:

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

    int tag = textField.tag;
    selectedTable = tag;
    float yy = 10;

    switch (tag) {
        case 0: 
            self.myPicker.hidden = NO;
            return NO;

        case 1:
            self.myPicker.hidden = NO;
            return NO;

        case 2:
            self.myPicker.hidden = NO;
            return NO;

        default:
            self.myPicker.hidden = YES;
            return YES;

        }        
}

Upvotes: 1

Related Questions