当 iPhone 上的 applicationDidBecomeActive 时,如何从活动实例设置 NSTimer 的目标

发布于 2024-11-13 11:52:01 字数 289 浏览 3 评论 0原文

我在 appDelegate 中创建了一个 NSTimer 来处理应用程序锁定或放入后台时的暂停,但计时器选择器位于另一个 viewController 中,当用户位于该 viewController 中然后单击主页按钮时,应用程序将关闭并移动到后台,但是当点击应用程序时,我需要在 applicationDidBecomeActive 中再次设置计时器,但不确定如何从应用程序委托访问活动 viewController,我尝试在viewController 的 viewDidAppear 但它没有被解雇,如果有人有一些关于如何处理这个问题的线索,我将非常感激。

I have an NSTimer that I created in the appDelegate to handle the pause when the app locks or is put in the background, but the timers selector is in another viewController, when the user is in that viewController and then hits the home button the app closes and moves to the background, but when the app is tapped I need to set the timer again, in the applicationDidBecomeActive, but am not sure how I access the active viewController from the app delegate, I have tried setting the timer in the viewController's viewDidAppear but it is not fired, if anyone has some clues as how to handle this I would be very grateful.

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

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

发布评论

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

评论(2

美男兮 2024-11-20 11:52:02

好吧,事实证明这很简单,我只是没有考虑清楚,可能有点太累了,无论如何答案与 David 提到的很接近,关键是循环遍历导航控制器中的 viewControllers 来获取正确的视图控制器

  for (UIViewController *vc in [self.navigationController viewControllers]) {
    if ([vc isKindOfClass:[CustomViewController class]]) {
      CustomViewController *fvc = (CustomViewController *)vc;
      self.sessionTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:fvc selector:@selector(saveTimesAndUpdateButtonDisplay) userInfo:nil repeats:YES];

      [[NSRunLoop currentRunLoop] addTimer:sessionTimer forMode:NSRunLoopCommonModes]; 
    }
  }

Well it turns out that this is pretty easy, I just wasn't thinking it through, maybe a bit too tired, anyway the answer is close to what David mentioned, the key is to loop through the viewControllers in the navigation controller to get the proper viewController

  for (UIViewController *vc in [self.navigationController viewControllers]) {
    if ([vc isKindOfClass:[CustomViewController class]]) {
      CustomViewController *fvc = (CustomViewController *)vc;
      self.sessionTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:fvc selector:@selector(saveTimesAndUpdateButtonDisplay) userInfo:nil repeats:YES];

      [[NSRunLoop currentRunLoop] addTimer:sessionTimer forMode:NSRunLoopCommonModes]; 
    }
  }
你如我软肋 2024-11-20 11:52:01

创建一个变量来在应用程序委托中存储 viewController...

或者,如果您使用的是 UINavigationController,则可以使用 [navigationController topViewController]

Create a variable to store viewController in the app delegate...

Or if you are using a UINavigationController you can use [navigationController topViewController]

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