gpt4 book ai didi

ios - becomeFirstResponder 正在触发 UIKeyboardDidHideNotification

转载 作者:行者123 更新时间:2023-11-28 22:13:14 25 4
gpt4 key购买 nike

我的应用程序的一个目标是管理多个自定义输入 View (来自 nibs)以及常规系统键盘。一个或另一个将永远存在。 (在示例代码中,我只管理一个 Nib + 系统键盘)。

更换键盘时,我想保留您在系统键盘上看到的滑入/滑出效果。为了获得自定义键盘和系统键盘的滑动动画,我从文本字段中 resignFirstResponder 并等到键盘被隐藏。然后我执行 becomeFirstResponder 以引入新键盘。我使用 UIKeyboardDidHideNotification 作为触发 becomeFirstResponder 并加载新键盘的触发器。

我看到的问题是 UIKeyboardDidHideNotification 触发了两次……一次是在执行 resignFirstResponder 时(如预期的那样),另一次是在执行 becomeFirstResponder 时。我怀疑我可以设置某种状态来检测第二个通知,但我想了解,为什么 becomeFirstResponder 首先导致它触发?

#import "DemoViewController.h"

@interface DemoViewController ()
@property (strong, nonatomic) IBOutlet UITextField *dummyTextField;
@end

@implementation DemoViewController

- (void)viewDidLoad
{
[super viewDidLoad];
[[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceDark];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

[self showNormalKeyboard];
[_dummyTextField becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)keyboardDidHide:(NSNotification*) notification {
NSLog(@"Keyboard hidden: %@", notification);
// Executing this next line causes the notification to fire again
[_dummyTextField becomeFirstResponder];
}

-(void)showAlternateKeyboard{
_dummyTextField.inputView = [[[NSBundle mainBundle] loadNibNamed:@"AlternateKeyboardView" owner:self options:nil] lastObject];
[_dummyTextField resignFirstResponder];
}

-(void)showNormalKeyboard{
_dummyTextField.inputView = nil;
[_dummyTextField resignFirstResponder];
}

// This is a button on the DemoViewController screen
// that is always present.
- (IBAction)mainButtonPressed:(UIButton *)sender {
NSLog(@"Main Screen Button Pressed!");
}

// This is a button from the Custom Input View xib
// but hardwired directly into here
- (IBAction)alternateKeyboardButtonPressed:(UIButton *)sender {
NSLog(@"Alternate Keyboard Button Pressed!");
}

// This is a UISwitch on the DemoViewController storyboard
// that selects between the custom keyboard and the regular
// system keyboard
- (IBAction)keyboardSelectChanged:(UISwitch *)sender {
if (sender.on){
[self showAlternateKeyboard];
} else {
[self showNormalKeyboard];
}
}

@end

最佳答案

我的猜测是您在前一个字段完全完成辞去第一响应者之前调用了 becomeFirstResponder。只是为了看看它是否有帮助,请尝试添加一些延迟性能:

-(void)keyboardDidHide:(NSNotification*) notification {
NSLog(@"Keyboard hidden: %@", notification);
dispatch_async(dispatch_get_main_queue(), ^{
[_dummyTextField becomeFirstResponder];
};
}

关于ios - becomeFirstResponder 正在触发 UIKeyboardDidHideNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22363362/

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