2010-04-21 2 views
0

아이디어가 매우 간단합니다. http 다운로드 클래스가 있어야합니다.이 클래스는 http 인증을 지원해야하지만, 기본적으로 백그라운드 스레드이므로 화면에 직접 묻지 않도록하고 싶습니다. delegate 메소드를 사용하여 viewController처럼 클래스 외부에서 요구할 수있다.@selector 및 반환 값

하지만 가능한지 또는 다른 구문을 사용해야하는지 모르겠습니다.

이 클래스는 사용이 위임 프로토콜 :이 대리자 메서드

//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:)]) { 
의 구현입니다

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

:

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



@interface Updater : NSThread { 
... 
} 

이 대리자 메서드 호출입니다

ObjC에서 th 메서드 이름에서 콜론 (:)이 중요합니다. 즉, authRequiredauthRequired:은 다른 방법입니다. 대신 다음을 시도하십시오.

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

hehehe, nice, thx :) – Cesar