gpt4 book ai didi

ios - 编辑另一个文本字段时隐藏键盘

转载 作者:行者123 更新时间:2023-11-29 11:05:11 24 4
gpt4 key购买 nike

我有 4 个文本字段,在编辑其中 3 个时,我希望使用标准键盘,但在第 4 个文本字段上,我想显示一个选择器轮。

当直接按下第 4 个文本字段时,我的代码工作正常,但如果我编辑文本字段 1-3,然后按下数字 4,键盘不会移除。

有什么想法吗?以及如何解决?

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:selectedCountryText])
{
NSLog(@"Edit country");
[self.view endEditing:YES];

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.3f];
if (pickerVisible)
{
picker.frame = CGRectOffset(picker.frame, 0, -341);
pickerVisible = FALSE;
}
else
{
picker.frame = CGRectOffset(picker.frame, 0, 341);
pickerVisible = TRUE;
}
[UIView commitAnimations];
[self removeKeyboard];
}
else
{
if (pickerVisible)
{
//Remove picker
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.3f];
picker.frame = CGRectOffset(picker.frame, 0, -341);
[UIView commitAnimations];
pickerVisible = FALSE;
}
}
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:selectedCountryText])
{
//do nothing for now
}
else
{
[self.view endEditing:YES];
[self removeKeyboard];
}
}

- (void) removeKeyboard
{
[self.firstname resignFirstResponder];
[self.lastname resignFirstResponder];
[self.year resignFirstResponder];
[self.country resignFirstResponder];
}

在 View 加载中我设置了委托(delegate)。

- (void)viewDidLoad
{
NSLog(@"viewdidload SearchSwimmerView");
// Setup the text fields..
[self.firstname setDelegate:self];
[self.firstname setReturnKeyType:UIReturnKeyDone];
[self.firstname addTarget:self action:@selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];

[self.lastname setDelegate:self];
[self.lastname setReturnKeyType:UIReturnKeyDone];
[self.lastname addTarget:self action:@selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];

[self.year setDelegate:self];
[self.year setReturnKeyType:UIReturnKeyDone];
[self.year addTarget:self action:@selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];

[self.country setDelegate:self];
}

最佳答案

而不是使用[self.view endEditing:YES] 使用

    [textField performSelector:@selector(resignFirstResponder)
withObject:nil
afterDelay:0];

关于ios - 编辑另一个文本字段时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13969852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com