【问题标题】:iPhone SDK - Asynchronous Download MethodsiPhone SDK - 异步下载方法
【发布时间】:2010-09-11 21:14:26
【问题描述】:

在 Erica Sadun 的下载助手 link here 中,我可以将这些方法放入我的类中:

- (void) didReceiveData: (NSData *) theData;
- (void) didReceiveFilename: (NSString *) aName;
- (void) dataDownloadFailed: (NSString *) reason;
- (void) dataDownloadAtPercent: (NSNumber *) aPercent;

这些方法显然参考了“DownloadHelper.h”和“DownloadherHelper.m”。我想知道我是否能够通过其中一种方法访问 xib 视图上的属性,例如文本字段、uitableviews、uilabels ......等。例如:

- (void) didReceiveFilename: (NSString *) aName {
    [label setText:@"hi"];
}

我已尝试这样做,但 xib 视图上的对象不会更新。就像上面给出的例子一样。有没有办法做到这一点?

谢谢,

凯文

【问题讨论】:

  • 您介意更新此问题以提供有关问题实际原因的一些详细信息(代码)吗?

标签: iphone objective-c xcode download object


【解决方案1】:

应该可以访问/更新您从 xib 链接到控制器的任何插座的属性。您是否通过调试器验证了didReceiveFilename: 消息正在发送到您的控制器,并且当setText: 消息发送给它时label 不是零?

编辑:我与离线发布此问题的人进行了沟通。该问题与异步下载无关。他的代码试图通知包含表格视图的控制器下载已经开始,但消息被发送到错误的控制器实例。解决这个问题可以解决问题。

从这里改变:

   // code that switched to an existing DownloadViewController omitted...
   // the new download controller that is created here has no views seen by the user!
   DownloadViewController *download = [[DownloadViewController alloc] init];
   [download downloadstart];

到这里:

   // code that switched to an existing DownloadViewController omitted...
   // use the existing DownloadViewController instead of creating a new one
   DownloadViewController *download = (DownloadViewController *)[[appDelegate.rootController viewControllers] objectAtIndex:1];
   [download downloadstart];

downloadstart 方法负责更新 Kevin 代码中的表格视图。

【讨论】:

  • 是的,我已经用断点调试过,它们都是肯定的。而不是 [label setText:@"hi"];代码我实际上是使用以下代码来制作一个单元格:[data addObject:name]; [自我保存数据]; [mainTableView reloadData];
【解决方案2】:

UI 更新发生在主线程上,你应该试试

执行SelectorOnMainThread:withObject:waitUntilDone:

[label performSelectorOnMainThread:@selector(setText:) 
           withObject:@"hi" waitUntilDone:NO];

【讨论】:

  • 你知道我认为那将是解决方案,但它仍然无法正常工作。我实际上并没有尝试更新标签,而是尝试在此处更新代码:stackoverflow.com/questions/3691074/… ... 这是使用 UITableView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
相关资源
最近更新 更多