ios - 无法保存 JSON 数据以供多个 UIView 使用

标签 ios objective-c json ipad jsonmodel

我在访问已拉入的 JSON 数据时遇到了一些问题。我正在使用 JSONModel 来获取 JSON 数据,如下所示:

在我的 LeftViewController.m 的顶部

@interface LeftViewController ()
{
    PostgresFeed* _feed;
}

然后在下面:

-(void)viewDidAppear:(BOOL)animated
{

JSONHTTPClient getJSONFromURLWithString:@"myurl" completion:^(NSDictionary *json,  JSONModelError *err) {
NSError *error = nil;

       _feed = [[PostgresFeed alloc] initWithDictionary:json error:&error];

       NSLog(@"Players: %@", feed.player);

       [self.tableView reloadData];

    }];
}

-(void)fetchedData:(NSData *)responseData
{
     NSError* error;
     NSDictionary* playerData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

     NSMutableDictionary* player = [playerData objectForKey:@"player"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        _feed.player = [NSMutableArray array];
    }
return self;
}

在我的 PostgresFeed.h 中

@property (nonatomic, strong) NSString *playerName;
@property (nonatomic, strong) NSString *currentScore;
@property (nonatomic, strong) NSArray *totalPenalties;
@property (nonatomic, strong) NSString *timePlayed;

我的 PostgresFeed.m 中什么也没有

我知道,当我这样做时,我会将我想要的所有数据获取到我的 LeftViewController 中,这是 MasterDetail 的 tableView 。当我查看 NSLog(@"Players: %@", feed.player); 时,我可以看出我正在从数据库中获取我想要的所有数据。

如何访问我知道必须填充 DetailViewController 的数据?我应该使用 NSUserDefaults 吗?我应该创建一个新类来获取、解析和保存这些数据吗?

我对这一切都很陌生,因此非常感谢教程或类似教程的说明。如果需要更多代码或详细信息,请告诉我。

****编辑****

按照 @soryngod 的建议应用 NSNotificationCenter 后,我从 RightViewController 中的 NSLog(@"%@", notification.userinfo); 获得以下输出:

    2013-07-04 12:20:26.208 PlayerTracking[25777:11303] {
    player =     (
                {
            currentScore = "4";
            totalPenalties =             (
            );
            id = 9;
            name = "Jakob Melon";
            timeStarted = "2013-06-05 19:56:10";
        },
                {
            currentScore = 16;
            totalPenalties =             (
            );
            id = 10;
            name = "John China";
            timeStarted = "2013-06-06 17:21:300";
        },
                {
            currentScore = 178;
            totalPenalties =             (
            );
            id = 11;
            name = "Jason Dog";
            timeStarted = "2013-06-07 19:26:10";
        },
                {
            currentScore = 1233;
            totalPenalties =             (
            );
            id = 12;
            name = "Fox Wolfe";
            timeStarted = "2013-06-05 19:56:10";
        },
                {
            currentScore = 234;
            totalPenalties =             (
            );
            id = 13;
            name = "Dakota Cool";
            timeStarted = "2013-06-05 19:56:10";
        },
                {
            currentScore = "34234";
            totalPenalties =             (
            );
            id = 14;
            name = "Max Face";
            timeStarted = "2013-06-05 19:00:30";
        },
                {
            currentScore = "2342";
            totalPenalties =             (
            );
            id = 15;
            name = "Jonatan Blah";
            timeStarted = "2013-06-05 18:00:30";
        },
                {
            currentScore = "234234";
            totalPenalties =             (
            );
            id = 16;
            name = "Thomas Bus";
            timeStarted = "2013-06-05 19:56:10";
        },
                {
            currentScore = 34566;
            totalPenalties =             (
            );
            id = 17;
            name = "Super Cake";
            timeStarted = "2013-06-05 17:51:30";
        },
                {
            currentScore = "23463";
            totalPenalties =             (
            );
            id = 18;
            name = "Duke Nukem";
            timeStarted = "2013-06-07 19:26:10";
        },
                {
            currentScore = "12362";
            totalPenalties =             (
            );
            id = 19;
            name = "Gordon Freeman";
            timeStarted = "2013-06-05 19:56:10";
        }
    );
}

请不要介意这些名字。

最佳答案

您可以使用 [NSNotificationCenter defaultCenter]userInfo 发布到 DetailController ,收到数据后,您只需使用 feed 发布通知并处理它在DetailController上。

更明确地说:

将其添加到 DetailViewController viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"myNotification" object:nil];

然后创建方法:

- (void) handleNotification:(NSNotification *) notification
{//Your information
NSLog(@"%@",notification.userInfo);
}

从哪里接收您发布的 JSON,如下所示:

[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:nil userInfo:yourDictionary];

请告诉我这是否有帮助。

关于ios - 无法保存 JSON 数据以供多个 UIView 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17460980/

相关文章:

javascript - 类型错误 : Cannot destructure property `stat` of 'undefined' or 'null'

ios - quartz 动画错误

ios - 无法使用文字整数访问我的 NSArray 但可以动态工作

json - 使用 jq 获取 JSON 实体流的第 n 个元素

iOS后台上传图片

ios - 错误?读取 IBAction 按钮内的变量

ios - Swift – 关闭 View Controller 后 TableView 数据未重新加载

ios - 我找不到在 UIAlertView 中按下的按钮

json - 在进行 PATCH 请求时,是否可以在 http header 中传递 json 字符串以向服务器提供选项?

javascript - 尝试解析大型 JSON 对象并提取数据,但不断收到意外的 token 错误