数据源(来自模型)未在视图控制器中加载

时间:2014-08-05 08:40:49

标签: ios objective-c arrays model-view-controller uilabel

我在模型(model.h和model.m)中保存了NSArray个字符串,数组中填充了TableViewController中的字符串,我可以成功{{1} }} editsave来自我的模型的数据。我想让字符串显示在access的{​​{1}}中,但它只显示(null),这意味着我没有成功访问模型中的数据,有人有帮助?

PS:我没有显示TableView的代码导致它正常工作。

这是我的代码:

Model.h:

UILabel

Model.m:

ViewController

ViewController.h

#import <Foundation/Foundation.h>

@interface Model : NSObject<NSCoding>

@property (strong, nonatomic) NSArray *array;



+(NSString *)getPathToArchive;
+(Model *)getModel;
+(void)saveModel:(Model *)aModel;


@end

ViewController.m

#import "Model.h"

@implementation Model

- (id)init {
    self = [super init];
    return self;
}

+(NSString *)getPathToArchive {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [paths objectAtIndex:0];
    return [docsDir stringByAppendingPathComponent:@"model.model"];
}

+(Model *)getModel{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[Model getPathToArchive]];
}

+(void)saveModel:(Model *)aModel {
    [NSKeyedArchiver archiveRootObject:aModel toFile:[Model getPathToArchive]];
}

-(void)encodeWithCoder:(NSCoder *)anEncoder {
    [anEncoder encodeObject:self.model forKey:@"array"];


}

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if(self) {
        self.model = [aDecoder decodeObjectForKey:@"array"];

    }
    return self;
}

@end

1 个答案:

答案 0 :(得分:0)

viewDidLoad中更改以下行:

    [Model getModel];

   self.model = [Mode getModel];

您的模型确实以getModel方法加载数据。但是,您仍然必须将此方法返回的Model实例分配给model的{​​{1}}属性。否则,您将无法访问ViewController