【问题标题】:I'd like to call when enter foreground in iOS [closed]我想在iOS中进入前台时打电话[关闭]
【发布时间】:2014-02-20 08:29:04
【问题描述】:

我只有一个GalleryViewController 作为UIViewController
它由情节提要加载。

我想在AppDelegate.m 中调用我的GalleryViewController 的一个方法。
但我在AppDelegate.m 中没有引用我的GalleryViewController

【问题讨论】:

标签: ios foreground


【解决方案1】:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    UIViewController *galleryViewController = self.window.rootViewController;
    [galleryViewController doSomething];
}

编辑:这只是一个示例,说明如何获取对 GalleryViewController 的引用,如果它是您的 rootViewController。如果您在 NavigationController 或 TabBarController 中,则需要先获取对您的 navigationController 或 tabBarController 的引用,然后将其级联为 viewControllers。

如果你有一个复杂的 VC 结构,订阅通知可能更容易,正如 Wain 指出的那样。这应该归功于他更快的反应。

【讨论】:

  • 前景,而不是背景
  • 当然,一定是盲人。编辑了答案。谢谢。
  • 您能否在答案中添加一个简短的解释。
【解决方案2】:

您可以从应用委托中的window 获取对根视图控制器的引用,然后投射或导航您想要的控制器(如果它在导航控制器中)。

尽管控制器可以将自己添加为UIApplicationWillEnterForegroundNotification 通知的观察者,但您不需要将此关系/知识添加到应用程序委托中。

【讨论】:

  • +1:You don't need to add this relationship / knowledge to the app delegate。如果应用程序委托不需要知道它,那么它不应该知道。只需按照建议将UIApplicationWillEnterForegroundNotification 的观察者添加到您的GalleryViewController 课程中即可。
猜你喜欢
  • 1970-01-01
  • 2022-11-23
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
相关资源
最近更新 更多