0

I have an application in which I have a webservice call that returns data as a dictionary. I want to access this dictionary in another view controller for loading the values into a table.

Can anybody demonstrate how to pass this response dictionary from one view controller to another?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
hacker
  • 8,919
  • 12
  • 62
  • 108

2 Answers2

3

You could define an NSDictionary property in your AnotherViewController and set it from the previous controller. I'll give a brief example below.

//.h
@interface AnotherViewController {

  NSDictionary *data;
}
@property(nonatomic,retain) NSDictionary *data;
@end

//.m
@implementation AnotherViewController
@synthesize data;

Now from the current controller, after initializing AnotherViewController you set the dictionary before presenting it.

AnotherViewController *controller = [[AnotherViewController alloc] init];
controller.data = myCurrentDictionary;

Now AnotherViewController has a data property with the value of the previous controller.

Hope this helps.

skram
  • 5,314
  • 1
  • 22
  • 26
1

I am assuming that the webservice is called because something happened (button clicked, viewDidLoad/viewDidAppear). If this is the case, passing a reference of the UIViewController to the webservice class is a perfect valid option. Keep in mind that for this relationship you should create a protocol, so on your webservice class you have something like this:

id<ViewControllerResponseProtocol> referenceToViewController;

This ViewControllerResponseProtocolwould define a method like this:

-(void)responseFromWebservice:(NSDictionary*)myDictionary;

So when the webservice class has build the NSDictionary you can the above method from the referenceToViewController:

[referenceToViewController responseFromWebservice:myDictionary];

If there isn't any kind of relationship between both, you use could NSNotificationCenter for it.

P.S: The solution of skram is perfectly valid if you already have the NSDictionary from the webservice on the initial UIViewController and now you want to pass it to a new UIViewController. Although I don't think that's what you want.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • as stated by the OP, he would like to "..`access` the dictionary in another view controller". Meaning he already has it. Don't think a downvote was called for that :) – skram Jun 12 '12 at 07:37
  • Has it where? If he has it on the `UIViewController`that started the action, he should **accept your answer**. – Rui Peres Jun 12 '12 at 07:39
  • `another view controller` means it's already in `a` view controller and he wants the data in `another one` – skram Jun 12 '12 at 07:46
  • Well don't worry then, I am sure your answer will be accepted. You are also assuming that the new view controller doesn't exist (you are creating it). – Rui Peres Jun 12 '12 at 07:47