2009-12-14 2 views
0

여러 파일 다운로드 진행률 값을 표 셀의 UIProgressView로 업데이트하려고합니다. 비동기 다운로드 작업을 수행하는 NSOperationQueue있는 FileDownloader 클래스가 있습니다. FileDownloader 클래스의 "delegate"를 사용하여 UI를 업데이트하려고합니다. 하지만 코드를 컴파일 할 수는 없습니다. FileDownloader는 Singleton입니다. 나는 근본적인 무언가를 이해하기 위해 실종되었는지 궁금해.여러 파일 다운로드를위한 UIProgressView를 업데이트하려면 (델리게이트 사용 방법)?

다음은 코드 설정입니다.

FileDownloader.h

#import < Foundation/Foundation.h > 
// declare protocol to accept progress status of file downloads 
@protocol FileDownloaderDelegate < NSObject > 
// this function will update the progressview and re-display the cell 
- (void) updateProgessWithCurrentValue:(NSNumber*)value totalValue:(NSNumber*)totalValue; 
@end 

@interface FileDownloader : NSObject { 
NSOperationQueue *operationQueue; // for file download operations 
id < FileDownloaderDelegate > delegate; // to send progess value to UI 
} 

@property (nonatomic, retain) NSOperationQueue *operationQueue; 
@property (nonatomic, assign) id < FileDownloaderDelegate > delegate; 


+(FileDownloader*) sharedInstance; // FileDownloader is Singleton with its standard methods 

// when download is progressing, the delegate function will be called like 
// [self.delegate updateProgessWithCurrentValue:10 totalValue:100]; 
// 

@end

MyTableViewCell.h

#import < UIKit/UIKit.h > 
#import < FileDownloader.h > 
@interface MyTableViewCell : UITableViewCell < FileDownloaderDelegate > { 
UIProgressView *progressView; 
} 

@property (nonatomic, retain) UIProgressView *progressView; 

// MyTableViewCell.m will have the implementation of 
// - (void) updateProgessWithCurrentValue:(NSNumber*)value totalValue:(NSNumber*)totalValue; 
// to update UI 

@end

첫째, MyTableViewCell에서 컴파일러 오류 "가 FileDownloaderDelegate에 대한 프로토콜 선언을 찾을 수 없습니다"있어요. 그래서 FileDownloaderDelegate의 프로토콜 선언을 별도의 .h 파일로 옮겨서 컴파일 할 수있게되었습니다. 그럼에도 불구하고 여전히 내 tableViewController에서 대리자를 할당 할 수 없습니다.

[[FileDownloader sharedInstance] setDelegate : myTableViewCell];

나는 (내가 "@synthesize 대리자를"가지고 있지만)가 대리자를 모르는 의미하는 " FileDownloader는 setDelegate 방법에 응답하지 않을 수"경고를 받았습니다. 나는 싱글 톤 또는 델리게이트 사용법에 대해 이해하지 못하고 있는지 궁금합니다.

답변

0

귀하의 +sharedInstance 메서드는 FileDownloader*이 아니라 ContentSyncer*을 반환합니다. 이는 setDelegate: not found 경고의 원인 일 수 있습니다.

또한 FileDownloaderDelegate 프로토콜을 FileDownloader.h에 정의 할 수 있고 정의해야합니다. MyTableViewCell.h에서이 헤더 파일을 가져 오되, 대괄호 대신 따옴표를 사용하십시오. 예 :

#import "FileDownloader.h" 
+0

답장을 보내 주셔서 감사합니다. ContentSyncer는 코드를 여기에 게시하기 쉽게 읽을 수 있도록 클래스의 이름을 변경했을 때 남은 것입니다. 나는 코드를 재 편집했다. 코드는 여기에서 제안에 따라 프로토콜 선언을 별도의 파일로 분리 한 후에 컴파일됩니다. http://stackoverflow.com/questions/1233371/how-to-conform-to-a-self-made-protocol 어딘가에 "순환 수입"이있을 수 있습니다. 그러나 나는 그것이 어디에서 일어나는 지 모른다. 이제 UI의 tableCell에서 업데이트를 얻을 수 있습니다. 델리게이트를 지정할 때 남은 유일한 것은 컴파일러 경고입니다. 탄스크! – tmin

관련 문제