UITextField与UIPickerView键盘错误

时间:2014-02-05 08:01:22

标签: objective-c uitextfield uipickerview

我只是想问一下当我按下UITextField时是否可以防止键盘弹出?我有一个UITextFieldUIPickerView如果我第一次按下UITextField它很好,它会显示UIPickerView,但之后我会选择UIPickerView然后再次按下文本字段,而不是再次显示UIPickerView它显示键盘?我点击UITextField后会显示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; } }
{{1}}

我该如何修复这种错误?非常感谢你!

2 个答案:

答案 0 :(得分:1)

实施此方法,不要忘记将textfield.delegate分配给您的控制器

- (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;

}

这应该适合你:

- (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;

        }        
}

答案 1 :(得分:0)

你在找这个吗?

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

因此,只需在inputView或其他地方为UITextField设置正确的viewDidLoad即可。类似的东西:

self.myTextField.inputView = self.myPicker