1

我在一个中有 3 个文本字段,我已经实现了一个UIPickerView并禁用了键盘resignFirstResponder,但是当我点击另一个时,键盘和pickerview 都是可见的。我该如何排序?

4

6 回答 6

2

创建 的本地实例UIPickerView,并在 TextFiled 的委托方法中textFieldDidBeginEditing检查 的可见性UIPickerView,如果它是可见的,则隐藏它

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(![_pickerView isHidden])
    {
        [_pickerView setHidden:YES];
    }
    //...
}
于 2013-04-29T08:41:21.690 回答
1

You need to know what to show exactly. You can use [self.view endEditing:YES]; in order to remove the keyboard when necessary, and implement your own method to hide the picker when needed.

于 2013-04-29T08:29:37.257 回答
1

最简单的解决方案:inputView 属性

yourTextField.inputView = self.yourPickerView;

检查文档

于 2013-04-29T11:19:10.127 回答
1

将每个UITextField的委托设置为self,然后将此方法放入您的类中。在这段代码中,我只是设置了带有点的.m框架,而不是您可以隐藏..yourPickerView500 yUIPickerView

此代码隐藏UIPickerView与键盘等动画..

更新:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
       if (textField == TextField1) {
           [UIView beginAnimations:nil context:NULL];
           [UIView setAnimationDuration:0.3];
           CGRect frame = yourPickerView.frame;
           frame.origin.y = 500;
           yourPickerView.frame = frame;
           [UIView commitAnimations];
           return YES;
        }
       else if (textField == TextField2) {
           [TextField1 resignFirstResponder];
           [UIView beginAnimations:nil context:NULL];
           [UIView setAnimationDuration:0.3];
           CGRect frame = yourPickerView.frame;
           frame.origin.y = 107;
           yourPickerView.frame = frame;
           [UIView commitAnimations];
           return NO;
        }
        return YES;
}
于 2013-04-29T09:31:46.303 回答
1

将选择器设置inputView为文本字段:

myTextField.inputView = self.myPickerView;

这样,您根本不需要处理键盘/选择器的显示。这一切都为你完成了。

于 2013-04-29T10:12:56.903 回答
0

当您要显示PickerView.

[self.view endEditing:YES];

现在之后 PickerView 将打开并且在编写以下行后焦点将不会在 TextField 上,因此您需要做的是将完成按钮放在 Picker 上并将 Picker 的 Selected 值传递给该 TextField。

试试这个代码:

- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{
    if(txtVisitDate.isEditing == TRUE)
    {
        if ( [txtPlaceName isFirstResponder] )
        {
            [txtPlaceName resignFirstResponder];
        }
        [aTextField resignFirstResponder];  

        pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];  
        pickerView.datePickerMode = UIDatePickerModeDate; 
        pickerView.hidden = NO;
        pickerView.date = [NSDate date];  

        UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
        pickerToolbar.barStyle = UIBarStyleBlackOpaque;  
        [pickerToolbar sizeToFit];  

        NSMutableArray *barItems = [[NSMutableArray alloc] init];    
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
        [barItems addObject:flexSpace];  

        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];  
        [barItems addObject:doneBtn];  

        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];  
        [barItems addObject:cancelBtn];  

        [pickerToolbar setItems:barItems animated:YES];  

        [pickerViewPopup addSubview:pickerToolbar];  
        [pickerViewPopup addSubview:pickerView];  
        [pickerViewPopup showInView:self.view];  
        [pickerViewPopup setBounds:CGRectMake(0,0,320, 475)];  
    }
}

希望这会有所帮助。

于 2013-04-29T08:47:55.573 回答