Search code examples
iosuiviewuiimageviewviewwithtag

Why does this UIImageView think it's a UIView?


In a view controller in a storyboard I have a simple view which has a subview image with tag 0 and a subview label with tag 1.

I'm trying to get the image like this:

UIImageView *myImage = (UIImageView*)[myView viewWithTag:0];

But when I do this:

myImage.highlighted = YES;

I get this:

-[UIView setHighlighted:]: unrecognized selector sent to instance

It is clearly a UIImageView in the storyboard. Why would this cast not work?


Solution

  • Because of [myView viewWithTag:0] which is the tag number for the main view as well

    Solution:

    • Give your UIImageView object a tag of, say, 100
    • Access it now as (UIImageView*)[myView viewWithTag:100]