我的根视图控制器中有一个数组,每当调用applicationDidEnterBackground时都需要保存(使用核心数据)。如何将消息发送到委托中的根视图控制器实例?

我正在考虑为委托提供一个引用,以将其设置为根,但感觉效率很低。有一个更好的方法吗?

最佳答案

1)在您的RootViewController .h文件中,

+ (RootViewController *) sharedStore;


2)在您的RootViewController .m文件中,

+ (RootViewController *) sharedStore
{
    static RootViewController *myStore = nil;

    if (!myStore) {
        myStore = [[RootViewController alloc] init];
    }
    return myStore;
}


3)转到您的AppDelegate.m文件并导入RootViewController

#import RootViewController.h


4)在您的AppDelegate.m文件中,转到ApplicationDidEnterBackground方法并键入:

[[RootViewController sharedStore] saveChanges];


5)在RootViewController中实现saveChanges方法。

关于ios - 如何将另一个 View Controller 的数组保存到应用程序委托(delegate)中的文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17256149/

10-12 00:19
查看更多