2010-08-20 42 views
0

长时间潜伏者,第一次海报。代表职能不被称为

我正在制作一个ServerConnection模块,使它成为一个模块化且更容易的模块,但无法使代理调用。我已经看到了更多这样的问题,但没有一个答案解决了我的问题。

ServerConnection设置为协议。因此,在Login.m中创建一个ServerConnection对象,该对象调用服务器,然后在Login中添加委托方法来处理是否有错误或者是否完成,这些由ServerConnection调用,如下所示。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    if([self.delegate respondsToSelector:@selector(connectionDidFinish:)]) { 
     NSLog(@"DOES RESPOND"); 
     [self.delegate connectionDidFinish:self]; 
    } else { 
     NSLog(@"DOES NOT RESPOND"); 
    } 

    self.connection = nil; 
    self.receivedData = nil; 

} 

它总是“不响应”。我已经尝试了CFRunLoop技巧(下面),但它仍然不起作用。

- (IBAction)processLogin:(id)sender { 

    // Hide the keyboard 
    [sender resignFirstResponder]; 

    // Start new thread 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    // Acutally call the server 
    [self authenticate]; 

    // Prevent the thread from exploding before we've got the data 
    CFRunLoopRun(); 

    // End thread 
    [pool release]; 

} 

我复制了Apple URLCache demo相当严重,并比较他们俩很多次,但无法找到任何差异。

任何帮助将不胜感激。

回答

2

这里有问题要问:

  • 贵委托connectionDidFinishLoading:应对?
  • 签名是否匹配,即它需要另一个对象?
  • 是否设置了代表或者是否为零? (用这种方法检查这个)

如果其中任何一个是“否”,你会看到“没有回应”......并且所有可能发生在你的应用程序中,但都很容易弄清楚。

+0

非常感谢。它缺少的 'self.delegate = theDelegate;' 在此函数 ' - (ID)initWithURL:(的NSString *)theURL withData:(的NSString *)海图代表:(ID )theDelegate; {' – user426140 2010-08-22 00:08:26