2009-12-14 38 views
0

我正嘗試將多個文件下載進度值更新爲表格單元格上的UIProgressView。我有FileDownloader類具有NSOperationQueue,它執行異步下載操作。我想用FileDownloader類中的「委託」更新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

首先,我得到了 「找不到FileDownloaderDelegate協議聲明」 中MyTableViewCell編譯器錯誤。於是我將FileDownloaderDelegate的協議聲明移出到單獨的.h文件中以便能夠進行編譯。即使如此,我仍然不能分配給我的tableViewController代理使用

[[FileDownloader sharedInstance] setDelegate:myTableViewCell];

我拿到「FileDownloader可能不setDelegate方法應對」的警告,這意味着它不知道代表(雖然我有「@synthesize代表」)。我想知道我是否不瞭解單身人士或代表使用情況。

回答

0

您的+sharedInstance方法返回ContentSyncer*而不是FileDownloader*。這可能是setDelegate: not found警告的原因。

另外,你可以也應該在FileDownloader.h中定義你的FileDownloaderDelegate協議。在MyTableViewCell.h中導入此頭文件,但使用引號而不是使用角括號。例如,

#import "FileDownloader.h" 
+0

感謝您的回覆。 ContentSyncer是我重命名這些類以使代碼更容易閱讀以供在此處發佈時的剩餘部分。我重新編寫了代碼。根據此處的建議,我將協議聲明分離爲單獨的文件後編譯了代碼。 http://stackoverflow.com/questions/1233371/how-to-conform-to-a-self-made-protocol 我讀過的地方可能會有一些「循環進口」。但我不知道發生了什麼。現在我可以從UI的tableCell獲取更新。當我分配委託時,唯一剩下的就是編譯器警告。 Thansk! – tmin 2009-12-15 05:34:10