我在基于选项卡的应用程序的选项卡上有一个UIViewController,它连接了UIApplicationDelegate。我想通过UIApplicationDelegate处理应用程序事件,但我没有在我的UIViewController中获取它们,它的方法没有被调用。
在我的UIViewController接口声明中将它连接起来我该怎么办?
@interface TestViewController : UIViewController<UIApplicationDelegate>
@end
其他代表是作品,我可以处理它们,除此之外,UIApplication及其代表应该有一些琐碎的'技巧',但我找不到。
谢谢!
答案 0 :(得分:9)
如果您需要app委托以外的类来响应各种应用程序事件,请让其他类注册各种应用程序通知。
例如,要处理进入后台的应用程序:
// Put this in the class that needs to deal with entering the background
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];
请参阅UIApplication
的文档以获取所有通知的列表。
不要忘记删除班级dealloc
方法中的观察者。