2010-04-21 86 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