0

I'm new to programming in general so please forgive my ignorance. If anyone can point me in the right direction it would be greatly appreciated.

I followed this tutorial for AFNetworking (http://www.raywenderlich.com/30445/afnetworking-crash-course) and have it working in my own project to parse JSON from the web.

I have yet to figure out how to transfer THAT data to a different UIViewController. I have seen Passing Data between View Controllers and other related questions to no avail.

I've tried using Delegates&Protocols/Singletons/GlobalVariables/ReferencingClasses and I can get them to work for other non JSON related material, when I NSLog the value it is always NULL. (Also the JSON data will be changing, and I read somewhere that some of these options are not mutable)

The tutorial seems to me to use custom NSDictionary classes to store the JSON Data and pass it over using prepareForSegue: but as far as I know, that is a Storyboard only method. I'm using nib files. Should I just switch my whole project to Storyboard style or is there a way to do this with Nibs/Xibs?

Thank you for taking the time to read this. The last thing I wanted to do is trouble you with a question but I've been stuck for a while now.

EDIT: I have retrieved JSON Data on the ViewController I preformed the request on, but it does not transfer over to other ViewControllers, and I feel it would be bad practice to request JSON data on every new Controller I need it on.

EDIT2 Here is some Code (I can post more if you want)

 

-(void)jsonData { NSLog(@"Loading JSON..."); NSURL *url = [NSURL URLWithString:@"http://fillerCode.com"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"station",@"1",@"previous_song", nil]; NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/scripts/MoreFillerCode" parameters:params]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest: request // JSON SUCCESS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"Getting Next Song..."); self.get_next_song = (NSDictionary *)JSON; RightViewController *rightViewController = [[RightViewController alloc] init]; rightViewController.get_next_song = self.get_next_song; [self updateViewStuff]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Data" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [av show]; NSLog(@"Operation Failed"); }]; [operation start]; NSLog(@"JSON Operation Started"); }
4

2 回答 2

1

你的主要问题:

我已经在执行请求的 ViewController 上检索了 JSON 数据,但它不会转移到其他 ViewController。

这个问题的原因:

RightViewController *rightViewController = [[RightViewController alloc] init];
rightViewController.get_next_song = self.get_next_song;

[self updateViewStuff];

您正在分配和实例化一个全新的视图控制器,然后在其上设置一个属性。在此之后,您再也不会提及rightViewController,它将在完成块结束时被释放。任何时候你 alloc-init 都是在创建一个新的,如果你不使用它,将它设置为 @property,或者将它推送到导航控制器上,它就会消失。

我不太了解您的应用程序的其余部分,无法为您提供解决此问题的代码,但这里有一个解决此类问题的一般思路:

  • 创建一个处理发送/接收数据的新类(NSObject,而不是 UIViewController)。把所有 AFNetworking 的东西放在这里。
  • 当视图控制器需要数据时,让它向这个新对象询问数据。数据应该返回给视图控制器。如果可用,对象可以使用保存的数据,如果不可用,则使用 AFNetworking 发出请求

这样,您的任何视图控制器都不需要相互了解 - 您不需要沿链传递数据,您可以在需要时从数据源访问它。

于 2013-09-16T19:45:02.610 回答
0

如果您想让您的示例工作,您需要执行以下两个步骤之一:

  1. 在@interface 中声明您的 RightViewController 并在初始化您的对象、设置属性然后进行 segue 时引用该声明。

  2. 在您创建 RightViewController 对象的同一块中进行 segue - 否则您会失去对它的引用。

于 2013-09-16T20:57:26.370 回答