2010-04-21 93 views
0

这个想法很容易,我有一个http下载类,这个类必须支持http认证,但它基本上是一个后台线程,所以我想避免直接提示到屏幕,我想使用委托方法从类外部请求,如viewController。@选择器和返回值

但我不知道是否可能或者如果我必须使用不同的语法。

此类使用该委托方案:

//Updater.h 
@protocol Updater <NSObject> 
-(NSDictionary *)authRequired; 
@optional 
-(void)statusUpdate:(NSString *)newStatus; 
-(void)downloadProgress:(int)percentage; 
@end 



@interface Updater : NSThread { 
... 
} 

这是调用委托方法:

//Updater.m 
// This check always fails :(
if ([self.delegate respondsToSelector:@selector(authRequired:)]) { 
    auth = [delegate authRequired]; 
} 

这是委托方法

//rootViewController.m 
-(NSDictionary *)authRequired; 
{ 
    // TODO: some kind of popup or modal view 
    NSMutableDictionary *ret=[[NSMutableDictionary alloc] init]; 
    [ret setObject:@"utente" forKey:@"user"]; 
    [ret setObject:@"password" forKey:@"pass"]; 
    return ret; 
} 

回答

1
if ([self.delegate respondsToSelector:@selector(authRequired:)]) { 
的实施

在ObjC中,th方法名称中的冒号(:)非常重要。这意味着authRequiredauthRequired:是不同的方法。试试这个:

if ([delegate respondsToSelector:@selector(authRequired)]) { 
+0

hehehe,nice,thx :) – Cesar 2010-04-21 10:45:03