Buns of Aluminum
Buns of Aluminum

Reputation: 2439

Sending data from a delegate to a view controller in Objective-C on an iPhone

Note: I've only been using Objective-C for a week.

Here's my end goal: I want to call out to a server and grab a json file that has urls and url descriptions in it. If I can't get that file, I want to show an error view. If I can get that file, I want to display its contents in a table view.

Restrictions: I'm doing this in a Cocoa Touch Static Library (by requirement) to be included in a larger app that will load it "like" an app.

What I'm doing right now is I'm using Reachability to check for a connection to the host. Then I'm opening an NSURLConnection for the file. Once the file is gotten, I parse the json using the json-framework. The datatype of the jsonObject is id (afaik that means *).

Currently, ALL of that is happening in the Delegate. If I get errors with connection or file retrieval, I set the rootView to the error view controller. Otherwise, I set the rootView to my other view.

I've tried the method of setting the jsonObject to extern in the view controller, but that didn't work. I tried setting a property in the view controller and setting the jsonObject in the controller after I create it, but the jsonObject is nil at that point and everything blows up with some error regarding incorrect selectors or something.

Am I even headed in the right direction with this? How SHOULD this be done?

EDIT

My view controller is typed as UINavigationController and I stick whichever view controller I end up using into it. When I try to call a setter in my view controller, I have to cast the UINavigationController to my view type to be able to see the setter, but when I run it, I get the following error:

SpringboardApplication[5850:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setJsonObject:]: unrecognized selector sent to instance 0x602ed20'

I am calling it as follows:

[(LU_SOCLINKS_RootViewController *) root_navigation_controller setJsonObject:jsonObject];

Upvotes: 0

Views: 411

Answers (1)

CharlieMezak
CharlieMezak

Reputation: 5999

By "delegate" you mean the app delegate? I assume that the view controller is in an instance variable in your app delegate, in which case you just need to create a setter method in the view controller that you can use to pass the data.

In the view controller:

.h

- (void)setData:(NSData/NSString/whatever *)data;

.m

- (void)setData:(NSData/NSString/whatever *)data {
    vcData = [data retain];
    // do stuff with the data
}

In the app delegate

[viewController setData:theData];

This is a simplistic answer. Am I understanding your problem correctly?

Upvotes: 1

Related Questions