我在一个中有 3 个文本字段,我已经实现了一个UIPickerView
并禁用了键盘resignFirstResponder
,但是当我点击另一个时,键盘和pickerview 都是可见的。我该如何排序?
6 回答
创建 的本地实例UIPickerView
,并在 TextFiled 的委托方法中textFieldDidBeginEditing
检查 的可见性UIPickerView
,如果它是可见的,则隐藏它
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(![_pickerView isHidden])
{
[_pickerView setHidden:YES];
}
//...
}
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.
将每个UITextField
的委托设置为self
,然后将此方法放入您的类中。在这段代码中,我只是设置了带有点的.m
框架,而不是您可以隐藏..yourPickerView
500
y
UIPickerView
此代码隐藏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;
}
将选择器设置inputView
为文本字段:
myTextField.inputView = self.myPickerView;
这样,您根本不需要处理键盘/选择器的显示。这一切都为你完成了。
当您要显示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)];
}
}
希望这会有所帮助。