Trouble saving JSON data for use by multiple UIViews [duplicate]

限于喜欢 提交于 2019-12-24 00:38:34

问题


I am having a bit of trouble accessing the JSON data that I have pulled in. I am using the JSONModel to get my JSON data, like this:

At the Top of my LeftViewController.m

@interface LeftViewController ()
{
    PostgresFeed* _feed;
}

And then down below:

-(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;
}

And in my PostgresFeed.h

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

And nothing in my PostgresFeed.m

I know that when I do it this way, I am getting all of the data that I want into my LeftViewController, which is the tableView of a MasterDetail. And when I look at the NSLog(@"Players: %@", feed.player); I can tell that I am getting all of the data that I want from the database.

How do I access this data that I know I have to populate my DetailViewController? Should I use NSUserDefaults? Should I create a new class to fetch, parse and hold onto this data?

I am new to this all, so a point to a tutorial, or tutorial like instructions are greatly appreciated. If any more code or details are needed, please let me know.

****EDIT****

After applying the NSNotificationCenter as suggested by @soryngod, I get the following output from my NSLog(@"%@", notification.userinfo); in my 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";
        }
    );
}

Please, don't mind the names.


回答1:


You could use [NSNotificationCenter defaultCenter] to post the userInfo to the DetailController , when the data is received you just post a notification to with the feed and you handle it on the DetailController.

To be more explicit:

you add this to your DetailViewController viewDidLoad:

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

then you create the method:

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

and from where you receive the JSON you post like this:

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

Let me know if this helps.



来源:https://stackoverflow.com/questions/17460980/trouble-saving-json-data-for-use-by-multiple-uiviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!