Search code examples
iosobjective-cuiimageviewobjective-c-blocksuielement

Accessing UIElements inside a block ios


I'm using a block for completion handler. How to access a imageView/UIElements inside a block,

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

Solution

  • look at this question Get UIImageView using Afnetworking and put it in an array

    as i understand you are using Afnetworking category for setting uiimages.

    Your image will be automaticaly set. look at the method "setImage..."

    Blocks here for example to reload table data.

    If you need to update ui/ access ui elements you need to do it in main thread

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

    if you have some instances (no matter UI or not) and you want access them in block you should add "__block" before it.

    e.g.

    __block NSString *myString = @"test";