UIAccelerometer、ViewControllers 和 TabViewController,选项卡之间切换和 MotionBegan 未调用

发布于 2024-11-04 18:33:34 字数 1238 浏览 0 评论 0原文

因此,我有一个基于 TabBarController 的应用程序,并且在我的一个选项卡中使用 UIAccelerometer。我看到的问题是,我可以很好地访问和使用加速度计,直到我切换选项卡并切换回来。举例来说,我的加速度计位于选项卡 1 上,我单击选项卡 2,然后返回选项卡 1,加速度计关闭。

在我的 ViewWillAppear 方法中,我将视图控制器设置为第一响应者,如下所示:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[self becomeFirstResponder];

[accelerometer setDelegate:self];
...}

加速度计是视图控制器的实例变量。所以我在 http://developer.apple 中阅读。 com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/EventHandlingiPhoneOS.pdf 本指南指出,如果将加速度计委托设置为 nil,手机会将其关闭以节省电池寿命。注意到,我从 viewWillDisappear: 方法中删除了这行代码,该代码现在非常小:

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
    //commented out the line below and no dice
    //[accelerometer setDelegate:nil];

if([[self tableView]isEditing])
    [[self tableView]setEditing:NO];
[self resignFirstResponder];

}

仔细阅读 SO 和网络,导致这方面的信息很少。我可以在这里提供的唯一其他信息是,如果我来回切换选项卡,我可以向后导航一页(它也有一个导航控制器,并且是导航中的最后一页)并向前加速计然后正常工作。在我看来,它并没有真正将视图控制器设置为第一响应者。我希望有一些明显的事情我没有到达这里。任何帮助将不胜感激!

So I have an application that is based on a TabBarController and in one of my tabs Im using the UIAccelerometer. The problem Im seeing is that I can access and use the accelerometer just fine until I switch tabs and switch back. Say for instance my accelerometer is on tab1, and I click tab2 and back to tab1 the accelerometer is off.

In my ViewWillAppear method Im setting the viewcontroller as firstresponder like so:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[self becomeFirstResponder];

[accelerometer setDelegate:self];
...}

accelerometer is my instance variable for the viewcontroller. So I was reading in http://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/EventHandlingiPhoneOS.pdf this guide that if you set the accelerometers delegate to nil the phone will turn it off to conserve battery life. Noting that, I removed that line of code from my viewWillDisappear: method which is now pretty small:

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
    //commented out the line below and no dice
    //[accelerometer setDelegate:nil];

if([[self tableView]isEditing])
    [[self tableView]setEditing:NO];
[self resignFirstResponder];

}

Perusing SO and the net has led to little info on this. The only other info I can give here is that if I switch tabs back and forth, I can navigate backwards one page(its also got a navigationController and is the last page in the navigation) and forwards the accelerometer then works normally. It seems to me that its not really setting the view controller as the first responder. I would hope there is just something obvious Im not getting here. Any help would be much appreciated!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑丑阿 2024-11-11 18:33:34

所以事实证明我需要重写 viewController 中的 viewDidAppear 和 viewDidDisappear 方法。

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];}

- (void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:animated];}

完成此操作后,在视图控制器之间切换后调用了motionBegan。希望这对那里的任何人都有帮助......

So it turns out that I needed to override viewDidAppear and viewDidDisappear methods in my viewController.

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];}

- (void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:animated];}

After doing this, the motionBegan was called after switching between viewControllers. Hope this helps anyone out there...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文