要更新 UIProgressView 以进行多个文件下载(如何使用委托)?

发布于 2024-08-14 16:51:07 字数 2025 浏览 12 评论 0原文

我正在尝试将多个文件下载进度值更新为表格单元格上的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-08-21 16:51:07

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

另外,您可以而且应该在 FileDownloader.h 中定义 FileDownloaderDelegate 协议。在 MyTableViewCell.h 中导入此头文件,但使用引号而不是尖括号。例如,

#import "FileDownloader.h"

Your +sharedInstance method returns a ContentSyncer*, not a FileDownloader*. That may be the cause of the setDelegate: 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.,

#import "FileDownloader.h"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文