WatchOS - 使所有wkinterfacecontroller可见的最佳方法

时间:2016-01-22 15:15:27

标签: int parameter-passing watchkit

在wkinterfacecontroller 1中,我定义了两个名为base [8] [8]和player [8] [8]的int值。使用for循环我为两者生成My值。 现在我想在wkinterfacecontroller 2和3中访问它们。

但我真的不知道如何。在nsuserdefaults中保存每个int并在其他控制器中加载有点奇怪......

1 个答案:

答案 0 :(得分:1)

创建一个保存数据的单件模型。从需要此数据的所有视图中引用此类。使用下面的代码,您可以通过调用[Model sharedModel]来引用“模型”。访问变量:[Model sharedModel].myVariableName

// Model.m
// This method will only create one instance of Model no matter how many times it's called. 
// Import its header and use this method to get its reference
+ (instancetype)sharedModel
{
    static Model *sharedModel;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedModel = [[self alloc] initPrivate];
    });
    return sharedModel;
}

// Raise exception to make sure you don't create more than one object of the Model class
- (instancetype)init {
    [NSException raise:@"Singleton"
                format:@"Use +[Model sharedModel]"];
    return nil;
}

// Private init method
- (instancetype)initPrivate
{
    if (self = [super init]) {
        // Set properties
    } 
    return self;
}