委托方法未被调用
我正在制作一个开源(github)帮助程序类,用于异步下载图像(我遇到了很大的麻烦)。
但是,我设置了委托方法来提醒委托图像已完成下载。问题是委托方法没有被调用。我正在设置委托和所有内容,但我不知道为什么会出现问题。
请看一下我的代码!我只发布了相关代码。
MKAsyncImageDownloader.h
@protocol MKAsyncImageDownloaderDelegate <NSObject>
@required
- (void)imageShouldFinishDownloading;
@end
@interface MKAsyncImageDownloader : NSObject {
id <MKAsyncImageDownloaderDelegate> delegate;
}
- (id) initWithDelegate:(id <MKAsyncImageDownloaderDelegate>) delegat;
@property (retain, nonatomic) id <MKAsyncImageDownloaderDelegate> delegate;
@end
MKAsyncImageDownloader.m
- (id) initWithDelegate:(id<MKAsyncImageDownloaderDelegate>) delegat {
self = [super init];
if (self) {
delegate = delegat;
}
return self;
}
- (void)imageAtURLHasDownloaded:(NSDictionary *)dict {
[downloadedImageArray addObject:[dict objectForKey:@"image"]];
[[self delegate] imageShouldFinishDownloading];
}
MKOperation.m NSOperation 的子类。 我分配/初始化 MKAsynImageDownloader 仅执行选择器。 代码:
- (void)start {
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:self.targetURL]];
if (image) {
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:image, self.targetURL, nil] forKeys:[NSArray arrayWithObjects:@"image", @"url", nil]];
MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] init];
[downloader performSelectorOnMainThread:@selector(imageAtURLHasDownloaded:) withObject:dict waitUntilDone:YES];
[dict release];
[downloader release];
}
[image release];
}
RootViewController.h
MKAsyncImageDownloader *loader;
RootViewController.m 只是为了展示我如何设置代表。
loader = [[MKAsyncImageDownloader alloc] initWithDelegate:self];
I am making an OpenSource (github) helper class for downloading images asynchronously (I had major trouble with).
However, I have delegate methods set up to alert the delegate that a image has finished downloading. The problem is that the delegate method is not getting called. I am setting the delegate and everything, but I don't have a clue why the problem is occurring.
Please take a look at my code! I have only posted the relevant code.
MKAsyncImageDownloader.h
@protocol MKAsyncImageDownloaderDelegate <NSObject>
@required
- (void)imageShouldFinishDownloading;
@end
@interface MKAsyncImageDownloader : NSObject {
id <MKAsyncImageDownloaderDelegate> delegate;
}
- (id) initWithDelegate:(id <MKAsyncImageDownloaderDelegate>) delegat;
@property (retain, nonatomic) id <MKAsyncImageDownloaderDelegate> delegate;
@end
MKAsyncImageDownloader.m
- (id) initWithDelegate:(id<MKAsyncImageDownloaderDelegate>) delegat {
self = [super init];
if (self) {
delegate = delegat;
}
return self;
}
- (void)imageAtURLHasDownloaded:(NSDictionary *)dict {
[downloadedImageArray addObject:[dict objectForKey:@"image"]];
[[self delegate] imageShouldFinishDownloading];
}
MKOperation.m
Subclass of NSOperation.
I alloc/init MKAsynImageDownloader to perform the selector only.
Code:
- (void)start {
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:self.targetURL]];
if (image) {
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:image, self.targetURL, nil] forKeys:[NSArray arrayWithObjects:@"image", @"url", nil]];
MKAsyncImageDownloader *downloader = [[MKAsyncImageDownloader alloc] init];
[downloader performSelectorOnMainThread:@selector(imageAtURLHasDownloaded:) withObject:dict waitUntilDone:YES];
[dict release];
[downloader release];
}
[image release];
}
RootViewController.h
MKAsyncImageDownloader *loader;
RootViewController.m
Just to show how I am setting the delegate.
loader = [[MKAsyncImageDownloader alloc] initWithDelegate:self];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的
start
方法中,您永远不会调用正确的 init 方法,您正在调用:您应该调用
然后您说您在其他地方设置了
loader
对象?这是两个独立的对象,您实际使用的对象就是上面引用的对象。那就是:
不做功
。您的 MKOperation 没有对具有委托集的
loader
类的引用。你说:
我认为你误解了你在这里所做的事情。您将创建一个全新的
MKAsynImageDownloader
实例,并在该实例上执行选择器,而不是在 RootController 中的loader
实例。您可能希望让MKOperation
在初始化期间获取MKAsyncImageDownloader
对象。编辑:
这是我指的“下载器”。在您的 MKOperation 的 start 中,
这是与 RootViewController 中的实例不同的实例,该实例没有委托集。这是您正在处理的工作,因此,这是尝试通知委托人的工作...但同样,它没有有委托集。
In your
start
method you are never calling your proper init method, you are calling:you should be calling
Then you say you set a
loader
object somewhere else? These are two separate objects, the one you actually seem to use is what is referenced above.That is:
does not make
work. Your MKOperation has no reference to your
loader
class that has the delegate set.You state:
I think you misunderstand what you are doing here. You are create a brand new instance of
MKAsynImageDownloader
and performing the selector on that instance, not theloader
instance that lives in your RootController. You probably want to haveMKOperation
take aMKAsyncImageDownloader
object during it init.EDIT:
This is the "downloader" I'm referring to. In your MKOperation's start
That is the separate instance from the one that lives in your RootViewController, this one does not have a delegate set. THIS is the one you are doing work on, therefore, this one is the one attempting to notify the delegate... but again, it doesn't have a delegate set.
您研究过 SDWebImage 吗?
有时,解决问题的最简单方法是使用无需维护的工作代码...
https:// github.com/rs/SDWebImage
Have you looked into SDWebImage?
Sometimes the easiest way to solve a problem is to use working code you don't have to maintain...
https://github.com/rs/SDWebImage