【问题标题】:How to handle UIApplicationDelegate methods in an UIViewController?如何处理 UIViewController 中的 UIApplicationDelegate 方法?
【发布时间】:2013-01-10 00:43:55
【问题描述】:

我在基于选项卡的应用程序的选项卡上有一个 UIViewController,它连接了 UIApplicationDelegate。我想通过 UIApplicationDelegate 处理应用程序事件,但我没有在我的 UIViewController 中获取它们,它的方法没有被调用。

除了在我的 UIViewController 的接口声明中连接它之外,我应该怎么做?

@interface TestViewController : UIViewController<UIApplicationDelegate>
@end

其他委托都是作品,我可以处理它们,除此之外,UIApplication 及其委托应该有一些琐碎的“技巧”,但我找不到。

谢谢!

【问题讨论】:

    标签: ios uiviewcontroller uiapplicationdelegate


    【解决方案1】:

    如果您需要应用委托以外的类来响应各种应用程序事件,则让其他类注册各种应用程序通知。

    比如处理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 方法中移除观察者。

    【讨论】:

    • 如果这是这样做的方法,那么我会做错事:当应用程序终止时,我只想将视图的文本框的文本保存到 NSUserDefaults。所以恐怕它不会在控制器的应用程序委托中处理终止事件。但是在哪里?
    • 把这个放到需要保存状态的视图控制器中。
    • @Tom...按照 rmaddy 说的做,然后在终止时最好的做法是将该文本保存到数据库并在再次启动应用程序时检索它...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多