【问题标题】:Accessing UIElements inside a block ios访问块 ios 内的 UIElements
【发布时间】:2014-03-28 07:12:29
【问题描述】:

我正在为完成处理程序使用一个块。如何访问块内的imageView/UIElements

     [storyImageView setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:Nil completed:^(UIImage *storyImage,NSError *error,SDImageCacheType cacheType)
   {
       //need to access storyImageView inside a block
   }];

【问题讨论】:

  • 这个问题没有意义。你提到的imageView和“UIElements”,它们是调用块的类的属性吗?
  • imageView 是 UIImageView 的一个实例......如果我们尝试访问 imageView 对象,则会警告在块内捕获 imageView 会导致保留周期
  • 你需要创建一个对 imageView 的弱本地引用

标签: ios objective-c uiimageview objective-c-blocks uielement


【解决方案1】:

看看这个问题 Get UIImageView using Afnetworking and put it in an array

据我了解,您正在使用 Afnetworking 类别来设置 uiimages。

您的图像将被自动设置。看方法“setImage...”

这里的块例如重新加载表数据。

如果您需要更新 ui/访问 ui 元素,您需要在主线程中完成

dispatch_async(dispatch_get_main_queue(), ^{
    //updates
});

如果你有一些实例(无论 UI 与否)并且你想在块中访问它们,你应该在它之前添加“__block”。

例如

__block NSString *myString = @"test";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多