我在访问我提取的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吗?我应该创建一个新的类来获取、解析和保留这些数据吗?
我对这一切都是新手,所以非常感谢教程或类似教程的说明。如果需要更多的代码或详细信息,请告诉我。
****编辑***
按照@soringod的建议应用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,当收到数据时,您只需将通知与提要一起发布到,并在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];
如果这有帮助,请告诉我。