iPhone 晃动问题:加载 viewController 时未调用 viewDidAppear
我正在尝试检测 iPhone 设备上的震动,因此根据我所读到的内容,我需要将视图控制器设置为 viewDidAppear 方法的第一响应者。该方法应该被自动调用,但由于某种原因该方法从未被调用。仅调用 viewDidLoad。
这是我的代码(我只放置了相关部分):
BuildHouseViewController.h:
@interface BuildHouseViewController : UIViewController {
BuildHouseViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view becomeFirstResponder];}
-(void)viewDidAppear:(BOOL)animated{
[self becomeFirstResponder];
[super viewDidAppear:animated];}
-(BOOL)canBecomeFirstResponder {
return YES;}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"shake");
if ( event.subtype == UIEventSubtypeMotionShake )
{ }
if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
[super motionEnded:motion withEvent:event];
}
我在 viewDidAppear 方法上添加了断点,但它从未被调用。从未检测到震动,我想这是因为从未调用此方法,因此视图控制器永远无法成为第一响应者。我不明白为什么会发生这种情况。
任何帮助将不胜感激。
编辑:
我使用以下方式从另一个视图调用视图:
[self.view addSubview:nextScreen.view];
视图显示在屏幕上
I'm trying to detect a shake on the iPhone device, so for what i've read I need to set my view controller as first responder on the viewDidAppear method. This method is supposed to be called automatically, but for some reason this method is never called. Only viewDidLoad is called.
this is my code (I put only the relevant parts):
BuildHouseViewController.h:
@interface BuildHouseViewController : UIViewController {
BuildHouseViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view becomeFirstResponder];}
-(void)viewDidAppear:(BOOL)animated{
[self becomeFirstResponder];
[super viewDidAppear:animated];}
-(BOOL)canBecomeFirstResponder {
return YES;}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"shake");
if ( event.subtype == UIEventSubtypeMotionShake )
{ }
if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
[super motionEnded:motion withEvent:event];
}
I added breakpoints on the viewDidAppear method and it is never called. Shakes are never detected, I suppose it is because as this methods are never called, so the view controller can never become first responder. I don't understand why this is happening.
Any help will be appreciated.
Edit:
I call the view from another view using:
[self.view addSubview:nextScreen.view];
The view is displayed on screen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢您的快速解答。
我发现了一些有趣的事情。我尝试加载相同的视图,但我以不同的方式遇到了问题,并且得到了不同的结果
- 正如我之前所说,如果我使用另一个视图调用它:
viewDidLoad 永远不会被调用,我无法检测到震动。
-现在,如果我使用 AppDelegate 调用它:
viewDidLoad 被调用!我能够检测到震动,但是这个解决方案是不可能的,我应该能够从另一个视图调用它
- 如果我使用以下命令从另一个视图调用它:
viewDidLoad 被调用!然而,我不想使用模态视图控制器,但它似乎是解决我的问题的唯一解决方案,检测到震动。
奇怪的是,第一种方法没有正确加载视图,这是一个错误吗?
Thanks for the quick answers.
I've found something interesting. I tried loading the same view I'm having problems with in different ways and I'm getting different results
-As I said before if I call it from another view using:
viewDidLoad is never called and I cannot detect shakes.
-Now if I call it from the AppDelegate using:
viewDidLoad is called!! and I am able to detect shakes, however this solution is not possible, I should be able to call it from another view
-If I call it from another view using:
viewDidLoad is called!! However I don't want to use a modal view controller, but it appears to be the only solution to my problem, shakes are detected.
It is strange that the first method doesn't load the view correctly, is it a bug??
[self成为FirstResponder]之类的东西实际上并没有使其成为第一响应者。当视图将成为第一响应者时,将调用该方法。所以这并不是按照你的想法去做。
其次,只有当视图确实出现时才会调用 viewDidAppear 。它出现在屏幕上吗?你告诉它在哪里显示?您需要将视图控制器的视图添加为另一个视图的子视图,或者推送到导航控制器堆栈上,或者作为模式视图推送。
The [self becomeFirstResponder] and the like don't actually make that become the first responder. The method gets called when the view is going to become the first responder. So that's not doing what you think it is.
Secondly, the viewDidAppear will only be called when the view, well, did appear. Is it showing up on the screen? Where are you telling it to be displayed? You need to either add the view controller's view as a subview of another view, or pushed onto a navigation controller stack, or pushed as a modal view.
viewDidAppear:(BOOL)animated 仅当视图由 UINavigationController 或 UITabBarController 显示时才会被调用。如果将视图控制器的视图添加到子视图(例如滚动视图或其他视图),则不会调用它。你可能会这么认为,但你错了。我自己刚刚被这个咬了。
viewDidAppear:(BOOL)animated only gets called when the view is shown by UINavigationController or UITabBarController. If you add a view controller's view to a subview (such as a scrollview or what have you), it won't get called. You would think it would but you'd be wrong. Just got bit by this myself.
根据@Genericrich的评论,您可以在将子视图放入自己之后手动调用viewDidAppear。
这对我有用。希望它对其他人有帮助。
Further to @Genericrich's comments, you can manually call viewDidAppear after you put the subview in yourself.
This worked for me. Hope it helps someone else.