要更新 UIProgressView 以进行多个文件下载(如何使用委托)?
我正在尝试将多个文件下载进度值更新为表格单元格上的 UIProgressView。我有 FileDownloader 类,它具有执行异步下载操作的 NSOperationQueue。我正在考虑使用 FileDownloader 类中的“委托”来更新 UI。但我无法编译代码。我有 FileDownloader 作为单例。我想知道我是否缺少理解一些基本的东西。
以下是代码的设置。
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共享实例] setDelegate:myTableViewCell];
我收到“FileDownloader 可能无法响应 setDelegate 方法”警告,这意味着它不知道委托(尽管我有“@synthesize delegate”)。我想知道我是否不了解单例或委托的使用。
I am trying to update the multiple file download progress value to UIProgressView on a table cell. I have the FileDownloader class which has NSOperationQueue that does asynchronous download operations. I am thinking to update the UI using a "delegate" from FileDownloader class. But I cannot compile the codes. I have FileDownloader as Singleton. I am wondering whether I am missing to understand something fundamental.
The following is the setup of the code.
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
First, I got "cannot find protocol declaration for FileDownloaderDelegate" compiler error in MyTableViewCell. So I moved out the protocol declaration of FileDownloaderDelegate to a separate .h file to be able to compile. Even then, I still cannot assign to the delegate from my tableViewController using
[[FileDownloader sharedInstance] setDelegate:myTableViewCell];
I got the "FileDownloader may not respond to setDelegate method" warning, meaning it does not know the delegate (although I have "@synthesize delegate"). I am wondering whether I am not understanding something about singleton or delegate usage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
+sharedInstance
方法返回ContentSyncer*
,而不是FileDownloader*
。这可能是导致setDelegate: not found
警告的原因。另外,您可以而且应该在 FileDownloader.h 中定义 FileDownloaderDelegate 协议。在 MyTableViewCell.h 中导入此头文件,但使用引号而不是尖括号。例如,
Your
+sharedInstance
method returns aContentSyncer*
, not aFileDownloader*
. That may be the cause of thesetDelegate: not found
warning.Also, you can and should define your FileDownloaderDelegate protocol in FileDownloader.h. Import this header file in MyTableViewCell.h, but use quotes instead of angel brackets. E.g.,