【发布时间】:2012-03-14 23:04:33
【问题描述】:
我已经解决这个问题将近 4 天了。
我认为这不是我的代码的问题,而是导致问题的应用程序的结构。
我正在尝试实现协议和委托以将数组从一个 NSObject(class) 获取到 ViewController。
我的代码几乎是从tutorial 中逐行复制的,唯一的区别在于我打开了 ARC,因此不得不将 (nonatomic, retain) 替换为 (strong) 并且没有使用 dealloc :)
话虽如此,它仍然没有将数据传递回视图控制器。 (非常烦人)我尝试了几十种不同的解决方案组合,我得到了帮助,但没有任何效果。这让我相信我的应用程序的结构或事物的初始化方式等可能存在错误,我现在将尝试解释。
当我的带有 tableview 的 viewcontroller 加载称为我的解析器类的委托的 viewdidload 方法时,一旦加载了 tableview 的第一个单元格,它就会调用我的连接类并告诉它从服务器下载一些数据。 在我的连接类中,我使用来自苹果库的 NSURLConnection 委托,在委托方法 connectionDidFinishLoading 中,已下载的数据被传递给我的解析器类(但是这是我认为它出错的地方,因为我再次声明了对象......我认为这是事情出错的地方)
这就是我从连接类调用解析器类的方式。
parserClass *myparser = [[EngineResponses alloc] init];
[myparser ReciveResponse:receivedData];
然后,一旦数据在我的解析器类中,它就会被解析,然后我尝试将数据传递给我的视图控制器。但它永远不会访问我设置的委托方法。
希望这就是问题所在,因为我只是不知道还有哪里出错了。 你觉得呢?
更新:这是我的代码 -
ViewController.h
#import "EngineResponses.h" //delegates & protocols
interface SearchViewController : UITableViewController <PassParsedData> {
//delegates to parser class
EngineResponses *engineResponses;
//..
ViewController.m
#import "EngineResponses.h"
//this is where I set up the delegate/protocol for the parser class
- (void)viewDidLoad
{
[super viewDidLoad];
//..
engineResponses = [[EngineResponses alloc] init];
[engineResponses setMydelegate:self];
//..
}
//this is where i set up and call the connection class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//..
if(indexPath.section == 0){
//..
if (indexPath.row == 0){
EngineRequests *engineRequests = [[EngineRequests alloc] init];
[engineRequests initalizePacketVariables:0 startCode:@"myReg" activationCode:@"myAct" methodName:@"GetStuff"];
//..
}
#pragma - Reciver methods
- (void)sendArray:(NSArray *)array
{
ICMfgFilterArray = array;
[self.tableView reloadData];
}
EngineRequests.m
//connection delegates etc..
//then I pass the data from the connection delegates over to the parser class
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
EngineResponses *engineResponses = [[EngineResponses alloc] init];
[engineResponses ReciveResponse:receivedData];
}
EngineResponses.h
@protocol PassParsedData
- (void)sendArray:(NSArray *)array;
@end
//..
id <PassParsedData> mydelegate;
//..
@property (strong) id <PassParsedData> mydelegate;
EngineResponses.m
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//..
[[self mydelegate]sendArray:filteredArray];
}
1
【问题讨论】:
-
代码的编辑版本比散文描述更容易诊断。
-
嗯,你在做类似
[myParser setDelegate:myViewController];的事情吗?另外,确实需要查看您的代码以提出更好的解决方案。 -
好的,我已经更新了我的代码.. 抱歉花了这么长时间,只是确保我的一切都完美无缺.. 所以 IMO 我认为错误是我将解析器对象一分为二地方...但我不确定这是否正确或如何解决。
-
好吧,你的“文件名”和你的类名匹配吗?或者 ParserClass 是您的 EngineResponses 吗?好像是。如果是这样,那么您无缘无故地分配了两次。在 connectionDidFinish 中创建一个新的。现在,它的 mydelegate 最初设置为 nil(我猜)。因此,当您向 mydelegate 发送 sendArray 消息时,该消息为 nil,然后您将消息发送到 nil 对象。这既不会导致编译器错误,也不会导致运行时错误,但它肯定不会执行方法 sendArray。确保您的连接类获得对解析器类对象的引用。
-
是的,engineRequest 是连接类,engineResponse 是解析器类。这些都是正确的。但为了清楚起见,我只是标记了代码部分。
标签: iphone ios delegates protocols