UIViewController可见回调 [英] UIViewController visible callback

查看:361
本文介绍了UIViewController可见回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iOS应用程序,当我有互联网连接时需要做一些事情,当我没有时。如果我还没有在某个时候,我会向用户显示一条消息,告诉我互联网并回来。问题是如何检测以下情况:

I am developing an iOS application where need to do some stuff when I have Internet connection and other, when I haven't. If I haven't at some point I will show a message to the user to give me internet and come back. The question it is how to detect the following situation:


  • 用户按两次Home键,进入多任务,设置并将连接到互联网

  • 用户通过多任务回到我的应用程序,但没有按任何东西

我知道我会收到AppDelegate的回调:

I know I will get callbacks to the AppDelegate:

- (void)applicationDidEnterBackground:(UIApplication *)application
- (void) applicationDidBecomeActive:(UIApplication *)application

但是代码(它不是由我启动的) )它非常大,我不想在那里处理UIViewController需要,如果有任何替代方案。

but the code ( it is not started by me) it is very big, and I don't want to handle there the UIViewController needs, if there is any alternative.

我的UIViewController's - (void)viewDidAppear:(BOOL)动画当用户回来时不会调用它。

My UIViewController's - (void)viewDidAppear:(BOOL)animated it isn't called when the user came back.

断点不是肯定会被激活!

The breakpoint it is not hited for sure!

任何有用的想法,除了AppDelegate?

Any usable ideas, except in AppDelegate?

推荐答案

您可以使用通知中心在视图控制器中收听 applicationDidEnterBackground

You can use the notification center to listen to applicationDidEnterBackground within the view controller:

[[NSNotificationCenter defaultCenter] addObserver: self
                                      selector: @selector(handleEnteredBackground:) 
                                      name: UIApplicationDidEnterBackgroundNotification
                                      object: nil];

viewDidLoad 中执行此操作。类似于 applicationDidBecomeActive

Do this in viewDidLoad. Similarily for applicationDidBecomeActive.

不要忘记在 viewDidUnload中以观察者身份移除自己

这篇关于UIViewController可见回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆