Objective-C - 将数据从NSObject子类发送到UIViewController [英] Objective-C – Sending data from NSObject subclass to an UIViewController
问题描述
我有一个 UIViewController
里面有一个 UITableView
。在这个视图控制器中,我想显示一些我从互联网上下载的数据。所以为此,我创建了一个名为 OfficesParser
的辅助类,它应该执行以下操作:
I have an UIViewController
which has a UITableView
inside. In this view controller I want to display some data that I have downloaded from the internet. So for this I have created a helper class called OfficesParser
which is supposed to do the following:
- 使用
从网上下载数据ASIHTTPRequest
- 使用JSON解析器处理数据
- 完成后,将数据发送回我的视图控制器
在我的视图控制器中我是在
ing和 -viewDidLoad
中分配 init
我的助手类,如下所示:
In my view controller I'm alloc
ing and init
ing my helper class in -viewDidLoad
like so:
self.officesParser = [[[OfficesParser alloc] init] autorelease]; //officesParser is a retained property
然后在 -viewWillAppear:
我为将启动下载过程的officesParser对象调用方法如下:
Then in -viewWillAppear:
I call the the method for the officesParser object that will start the download process like so:
[self.officesParser download];
在我的助手班级 OfficesParser
ASIHTTPRequest
有一个委托方法,可以告诉您队列何时完成下载。所以从这个方法我想将数据发送到我的视图控制器。我认为这会有效,但它没有:
In my helper class OfficesParser
ASIHTTPRequest
has a delegate method that tells you when a queue has finished downloading. So from this method I want send the data to my view controller. I would think this would work, but it didn't:
- (void)queueFinished:(ASINetworkQueue *)queue {
NSArray *offices = [self offices];
OfficesViewController *ovc = [[OfficesViewController alloc] init];
[ovc setOffices:offices];
}
考虑到这些代码,你将如何实现我的目标我试图用正确的代码吗?
With this code in mind, how would you achieve what I'm trying to do with proper code?
推荐答案
你需要看一下代表和协议。它们正是您正在寻找的,因为它们让课程无需持久引用即可进行通信。 此处是对他们的另一种解释。
You need to take a look at delegates and protocols. They're exactly what you're looking for, as they let classes communicate without having to persist a reference. Here is another explanation on them.
这篇关于Objective-C - 将数据从NSObject子类发送到UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!