简体   繁体   English

成为FirstResponder似乎仅在第一次摇动手势时起作用

[英]becomeFirstResponder seems to work only 1st time for shake gesture

I'm making my app shake-gesture-compatible by doing this in my UIViewController: 我通过在UIViewController中执行以下操作使我的应用程序与手势兼容:

- (void)viewWillAppear:(BOOL)animated
{
    [self.view becomeFirstResponder];
    [super viewWillAppear:animated];
}

The problem is that when I flip to another view (I'm using the "Utility App" template which has a flipside view and a root view controller to manage them both) the shake gesture ceases to work when I come back. 问题是,当我翻转到另一个视图时(我使用的是“ Utility App”模板,该模板具有一个翻转视图和一个用于管理这两个视图的根视图控制器),当我回来时,摇动手势将停止工作。

I see that the viewWillAppear method is called, it just doesn't seem that the view regains first responder status the second time around. 我看到viewWillAppear方法被调用了,只是似乎该视图没有第二次重新获得第一响应者状态。

谁知道..您必须将调用to becomeFirstResponder放在viewDidAppear而不是viewWillAppear

Yup, that confirms it for me ... I was trying to set the controllers as firstResponder from the application delegate. 是的,这对我来说是肯定的。我试图将控制器设置为应用程序委托中的firstResponder。 Meanwhile, I found it very helpful for debugging to sprinkle this log statement in strategic methods. 同时,我发现以策略方法添加此日志语句对于调试非常有帮助。

NSLog(@"%s: I %s first responder! (%@)", __FUNCTION__, [self isFirstResponder] ? "am" : "am not", self);

Also, to allow me to trigger it at arbitrary times, I enabled the "orientation" method and put it in there. 另外,为了允许我在任意时间触发它,我启用了“方向”方法并将其放在其中。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSLog(@"%s: I %s first responder! (%@)", __FUNCTION__, [self isFirstResponder] ? "am" : "am not", self);
return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM