UIViewController 可见回调 [英] UIViewController visible callback

查看:23
本文介绍了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:

  • 用户按两次主页按钮,进入多任务、设置并连接到互联网
  • 用户返回到我的应用程序进行多任务处理,但没有按下任何按钮

我知道我会收到 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 的 - (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 中将自己作为观察者移除.

Don't forget to remove yourself as an observer in viewDidUnload.

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

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