简体   繁体   English

在iPhone上的Objective-C中将数据从委托发送到视图控制器

[英]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. 注意:我只使用了一周的Objective-C。

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. 这是我的最终目标:我想调出服务器并获取其中包含url和url描述的json文件。 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. 限制:我正在Cocoa Touch静态库中执行此操作(根据要求),以包含在更大的应用程序中,该应用程序将像“加载”应用程序一样加载它。

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. 然后,我为文件打开一个NSURLConnection。 Once the file is gotten, I parse the json using the json-framework. 获取文件后,我将使用json-framework解析json。 The datatype of the jsonObject is id (afaik that means *). jsonObject的数据类型为id(表示*的afaik)。

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. 如果在连接或文件检索时遇到错误,则将rootView设置为错误视图控制器。 Otherwise, I set the rootView to my other view. 否则,我将rootView设置为其他视图。

I've tried the method of setting the jsonObject to extern in the view controller, but that didn't work. 我已经尝试了在视图控制器中将jsonObject设置为extern的方法,但这没有用。 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. 我尝试在视图控制器中设置属性并在创建后在控制器中设置jsonObject,但那时jsonObject为nil,并且由于错误的选择器或其他内容,所有内容都会崩溃。

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. 我的视图控制器键入为UINavigationController,我将最终使用的任何视图控制器都粘贴到其中。 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: 当我尝试在视图控制器中调用设置器时,必须将UINavigationController强制转换为视图类型才能看到设置器,但是当我运行它时,出现以下错误:

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];

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. 我假设视图控制器位于应用程序委托的实例变量中,在这种情况下,您只需要在视图控制器中创建一个setter方法即可用于传递数据。

In the view controller: 在视图控制器中:

.h 。H

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

.m .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? 我能正确理解您的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM