无法保存JSON数据以供多个UIViews使用
我遇到了一些麻烦,访问我拉入的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吗? 我应该创build一个新的类来获取,parsing并保存这些数据吗?
我是新来的这一切,所以点教程,或像指导教程,非常感谢。 如果需要更多的代码或细节,请让我知道。
****编辑****
按照@soryngod的build议应用NSNotificationCenter之后,我从NSLog(@"%@", notification.userinfo);
得到以下输出NSLog(@"%@", notification.userinfo);
在我的RightViewController中:
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];
那么你创build的方法是:
- (void) handleNotification:(NSNotification *) notification {//Your information NSLog(@"%@",notification.userInfo); }
并从您收到的JSON你发布这样的:
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:nil userInfo:yourDictionary];
让我知道这是否有帮助。