UIResponder: keyCommands未被调用;isFirstResponder确认 &g



我的应用程序有一个密码视图,概念上类似于iOS的解锁屏幕。它是一个UIViewController我在一个新的UIWindow中呈现。工作很好。我增加了使用硬件键盘输入密码的功能。keyCommands方法不会被调用,因此按键不会被识别,直到用户在屏幕上的任何地方点击至少一次。它是一个全屏UIWindow/UIViewController所以假设它是UIWindow/UIViewController中的一个tap。一旦点击发生,keyCommands将按预期被调用,并且一切工作都很完美。我不想要求用户在输入密码之前点击他们的屏幕。

你知道这里发生了什么吗?特别是为什么用户需要点击屏幕(以及如何避免这个要求)?

我已经通过包含一个重复的NSTimer调用来验证UIViewController是firstResponder。

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
[NSTimer scheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer * _Nonnull timer) { //This is just debugging code!
[self confirmFirstResponder];
}];
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)confirmFirstResponder { //Caveman debugging at its finest
if ([self isFirstResponder]) {
NSLog(@"I'm first responder!"); //This is always logged repeatedly
} else {
NSLog(@"I'm NOT THE FIRST RESPONDER!!!!"); //This is never logged
}
}
-(NSArray<UIKeyCommand *> *)keyCommands {
NSLog(@"keyCommands fired"); //This is not fired until user taps the screen, then presses a key on the hardware keyboard
NSArray<UIKeyCommand *> *commands = @[
[UIKeyCommand commandWithTitle:@"1" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"1" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"2" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"2" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"3" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"3" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"4" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"4" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"5" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"5" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"6" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"6" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"7" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"7" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"8" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"8" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"9" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"9" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"0" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"0" modifierFlags:0 propertyList:nil],
[UIKeyCommand commandWithTitle:@"Delete" image:nil action:@selector(buttonPressedWithKeyCommand:) input:@"b" modifierFlags:0 propertyList:nil]
];
return commands;
}
下面是创建UIWindow的代码:
-(void)displayNewWindowWithViewController:(UIViewController *)vc {
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:vc]; //vc is the UIViewController containing the code above
nav.navigationBarHidden=YES; //I have no recollection why I'm wrapping the vc in a UINavigationController...
self.modalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.modalWindow.backgroundColor = [UIColor whiteColor];
self.modalWindow.clipsToBounds = NO;
self.modalWindow.rootViewController = nav;
self.modalWindow.windowLevel=99;
[self makeKeyAndVisible:self.modalWindow];
}
-(void)makeKeyAndVisible:(UIWindow *)window {
window.backgroundColor = [UIColor clearColor];
window.frame=CGRectMake(0, [UIScreen mainScreen].bounds.size.height, window.frame.size.width, window.frame.size.height);
[window makeKeyAndVisible];
window.frame=CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
}

除了我问的硬件键盘代码外,我在这里写的所有东西都是七年前写的。所以我不记得具体的逻辑了。我确实知道我使用了一个UIWindow因为这是一个安全视图,它绝对必须在所有其他视图之上,包括一些应用可能在这个可见时添加的视图。不管理想与否,它一直运行得很好。如果需要进行实质性的重构以使硬件键盘在这里工作,则硬件键盘功能将被丢弃。

新的ui窗口不完全是'键窗口';在捕获键盘事件方面,即使使用makeKeyandVisible指令。我通过临时将相同的代码添加到应用程序的主UIWindow上的UIViewController来验证这一点。它接收键盘事件,直到我点击屏幕(新的UIWindow)。

我改了这个:

[window makeKeyAndVisible]

:

window.hidden = NO;
dispatch_async(dispatch_get_main_queue(), ^{
[window makeKeyWindow];
});

,突然键盘完全被我的新UIWindow捕获了。

我不太清楚发生了什么事。原来的[window makeKeyAndVisible]肯定是在主线程上运行的(通过[NSThread isMainThread]验证)。但把它扔进另一个运行循环就成功了。图。

相关内容

  • 没有找到相关文章

最新更新