我有 2 个文本字段,我正在使用每个文本字段,单击时会打开一个带有顶部工具栏的选择器轮,可以选择关闭选择器并调出键盘,除非您关闭选择器并调出键盘,否则一切正常然后单击下一个文本字段。我把键盘放在上面,后面有新的pickerview。让键盘消失的唯一方法是在第一个文本字段中单击返回,然后单击完成或屏幕上的任何位置(不是文本字段)。
这是我的代码:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
for (NSUInteger i = 0; i<[self.fieldsArray count]; i++) {
if ([self.fieldsArray objectAtIndex:i] == textField) {
UITextField *input = [self.fieldsArray objectAtIndex:i];
if (input.tag == 3 && !self.overrideDriver) {
[self animatePickDriverForInput:input];
}
if (input.tag == 4 && !self.overrideVehicle) {
[self animatePickVehicleForInput:input];
}
}
}
}
以下是使用的其他一些方法:
- (IBAction)textFieldFinished:(id)sender
{
[sender resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)animatePickDriverForInput:(UITextField *)input
{
if ([self.drivers count] > 0) {
[self.view endEditing:YES];
[self showPickDriver];
} else {
//untested
[input addTarget:self action:@selector(textFieldFinished:)
forControlEvents:UIControlEventEditingDidEndOnExit];
}
}
- (void)animatePickVehicleForInput:(UITextField *)input
{
if ([self.vehicles count] > 0) {
[self.view endEditing:YES];
[self showPickVehicle];
} else {
//untested
[input addTarget:self action:@selector(textFieldFinished:)
forControlEvents:UIControlEventEditingDidEndOnExit];
}
}
- (void)allowManualEntryOfDriver
{
[self.additionalButtonPickerHelper animateDismiss:self.pickDriver];
self.overrideDriver = YES;
[self.driver becomeFirstResponder];
}
- (void)allowManualEntryOfVehicle
{
[self.additionalButtonPickerHelper animateDismiss:self.pickVehicle];
self.overrideVehicle = YES;
[self.vehicle becomeFirstResponder];
}
- (void)showPickVehicle {
self.pickVehicle = [self.additionalButtonPickerHelper createPicker:self.pickVehicle WithTag:2 WithOtherButtonText:@"Add Other Vehicle"];
[self.additionalButtonPickerHelper showPicker:self.pickVehicle WithDoneAction:@selector(dismissVehiclePicker) OrWithOtherAction:@selector(allowManualEntryOfVehicle)];
}
- (void)showPickDriver {
self.pickDriver = [self.additionalButtonPickerHelper createPicker:self.pickDriver WithTag:1 WithOtherButtonText:@"Add Other Driver"];
[self.additionalButtonPickerHelper showPicker:self.pickDriver WithDoneAction:@selector(dismissDriverPicker) OrWithOtherAction:@selector(allowManualEntryOfDriver)];
}
编辑:
更多代码:
- (void)dismissDriverPicker
{
[self.additionalButtonPickerHelper animateDismiss:self.pickDriver];
}
- (void)dismissVehiclePicker
{
[self.additionalButtonPickerHelper animateDismiss:self.pickVehicle];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
这可能不是最有效的方法,但我是目标 c 的新手,正在寻找我能理解的解决方案。谢谢!
编辑这里是一张图片