【问题标题】:Delegate not being set correctly未正确设置代表
【发布时间】:2014-08-16 18:41:01
【问题描述】:

我正在做一个项目,我的 UIWebview 类需要从我的 DownloadView 类中执行一个方法

我正在使用开源项目https://github.com/robertmryan/download-manager

当这段代码执行方法时:

 DownloadTableView *download = [[DownloadTableView alloc] init];
 [download queueAndStartDownloads:_downloadURL];

此行没有设置代理权限

  self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];

全启动下载方法

- (void)queueAndStartDownloads:(NSURL *)url
{


NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *downloadFolder = [documentsPath stringByAppendingPathComponent:@"downloads"];

if ([[NSFileManager defaultManager] fileExistsAtPath:downloadFolder])       //Does file exist?
{
    if (![[NSFileManager defaultManager] createDirectoryAtPath:downloadFolder
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:nil]) {

    }
}

self.downloadManager = [[DownloadManager alloc] initWithDelegate:self];
self.downloadManager.maxConcurrentDownloads = 4;


    NSString *downloadFilename = [downloadFolder stringByAppendingPathComponent:[url lastPathComponent]];
    [self.downloadManager addDownloadWithFilename:downloadFilename URL:url];


self.cancelButton.enabled = YES;
self.startDate = [NSDate date];
NSLog(@"DOwnling");
[self.downloadManager start];

}

我的 DownloadView 类中的方法不会执行

 - (void)didFinishLoadingAllForManager:(DownloadManager *)downloadManager

{

【问题讨论】:

  • 您没有显示足够的代码来确定问题所在。但是一般不要做“initWithDelegate”方法,这不是通常的做事方式。为您的类(或子类)使用指定的初始化程序,然后分配委托。
  • 我正在使用这家伙下载管理器github.com/robertmryan/download-manager
  • 如果需要委托,那么我认为指定的初始化程序 initWithDelegate 没有任何问题。
  • 当然,您完全可以忽略约定。但是,如果您想与他人合作,像 Apple 和其他人一样做这些事情会有所帮助。
  • “没有设置代理权”对您意味着什么?你期望会发生什么?您观察到什么让您感到惊讶?

标签: ios iphone inheritance delegates delegation


【解决方案1】:

假设您的代码在 ARC 下,从代码中我了解到DownloadTableView *download 是一个局部变量。因此,DownloadTableView 对象在声明它的方法范围结束后被释放。因此委托方法不会被调用,因为委托已被释放。为避免这种情况,您可以创建 DownloadTableView 对象作为实例变量。

【讨论】:

  • 感谢您的建议,但表格视图仍然没有更新任何想法,为什么?
  • 你在调用 reloadData 吗?
猜你喜欢
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
  • 2020-12-21
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多