在 iPhone 上的 Objective-C 中将数据从委托发送到视图控制器
注意:我只使用 Objective-C 一周。
这是我的最终目标:我想调用服务器并获取一个包含 url 和 url 描述的 json 文件。如果我无法获取该文件,我想显示错误视图。如果我可以获得该文件,我想在表视图中显示其内容。
限制:我在 Cocoa Touch 静态库中执行此操作(根据要求),以便将其包含在更大的应用程序中,该应用程序将“像”应用程序一样加载它。
我现在正在做的是使用 Reachability 来检查与主机的连接。然后我打开该文件的 NSURLConnection。获取文件后,我使用 json-framework 解析 json。 jsonObject 的数据类型是 id (据我所知,这意味着 *)。
目前,所有这一切都发生在 Delegate 中。如果我在连接或文件检索时遇到错误,我会将 rootView 设置为错误视图控制器。否则,我将 rootView 设置为我的其他视图。
我尝试过在视图控制器中将 jsonObject 设置为 extern 的方法,但这不起作用。我尝试在视图控制器中设置一个属性,并在创建它后在控制器中设置 jsonObject,但此时 jsonObject 为零,并且所有内容都会因选择器不正确或其他错误而崩溃。
我是否朝着正确的方向前进?这应该怎么做?
编辑
我的视图控制器类型为 UINavigationController,我将最终使用的任何视图控制器粘贴到其中。当我尝试在视图控制器中调用 setter 时,我必须将 UINavigationController 转换为我的视图类型才能看到 setter,但是当我运行它时,出现以下错误:
SpringboardApplication[5850:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setJsonObject:]: unrecognized selector sent to instance 0x602ed20'
我按如下方式调用它:
[(LU_SOCLINKS_RootViewController *) root_navigation_controller setJsonObject:jsonObject];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“委托”是指应用程序委托吗?我假设视图控制器位于应用程序委托中的实例变量中,在这种情况下,您只需在视图控制器中创建一个可用于传递数据的 setter 方法。
在视图控制器中:
.h
.m
在应用程序委托中
这是一个简单的答案。我正确理解你的问题吗?
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
.m
In the app delegate
This is a simplistic answer. Am I understanding your problem correctly?