【问题标题】:UIResponder: keyCommands not called; confirmed isFirstResponderUIResponder:未调用 keyCommands;确认 isFirstResponder
【发布时间】:2021-05-31 06:20:47
【问题描述】:

我的应用有一个密码视图,在概念上类似于 iOS 解锁屏幕。这是我在新 UIWindow 中呈现的 UIViewController。工作正常。我正在添加使用硬件键盘输入密码的功能。不会调用keyCommands 方法,因此在用户至少点击屏幕上的任意位置一次之前,无法识别按键。它是一个全屏 UIWindow/UIViewController,所以推测它是 UIWindow/UIViewController 中的一个点击。一旦点击发生,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,因为这是一个安全视图,它绝对必须高于所有其他视图,包括应用程序可能在可见时添加的一些视图。理想与否,它一直在完美运行。如果需要进行实质性的重新架构以使硬件键盘在此处工作,则硬件键盘功能将被删除。

【问题讨论】:

  • 为了它的价值:数字“按钮”是 UIButtons。它们按预期在第一次按下时触发。用户点击以启用键盘输入可以在视图的任何位置,无论是在 UIButton 中还是在视图的任何“惰性”区域中。
  • 您不应该以这种方式滥用辅助 UIWindow。但是,如果您确实创建了一个窗口,则将其设为关键取决于您。在我看来,水龙头正在走那一步。但是,您没有向我们展示任何真正重要的代码,即如何创建此窗口并使其成为关键。就我个人而言,我只想说:不要那样做,使用呈现的视图控制器,一切都会好起来的。
  • 谢谢@matt。我在问题中添加了 UIWindow 代码。它是 keyAndVisible。
  • 好吧,我重复我的建议:不要那样做。只需使用呈现的视图控制器。您可以随意调整视图的大小和位置,因此使用 UIWindow 没有任何优势。

标签: ios uiresponder uikeycommand


【解决方案1】:

就捕获键盘事件而言,新的 UIWindow 并不是“关键窗口”,即使使用 makeKeyandVisible 指令也是如此。我通过将相同的代码临时添加到应用程序主 UIWindow 上的 UIViewController 来验证这一点。它接收键盘事件,直到我点击屏幕(新的 UIWindow)。

我改变了这个:

[window makeKeyAndVisible]

到这里:

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

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

我不太确定发生了什么。原来的[window makeKeyAndVisible] 肯定是在主线程上运行的(用[NSThread isMainThread] 验证)。但是把它扔到另一个运行循环中就可以了。去图吧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多