Strong Like Bull
Strong Like Bull

Reputation: 11297

How to test for an empty UIImageView?

I have the following code where I want to see if a particular UIImageView (image) is set. If not then I want to display an error message.

if (image==nil) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"No Image Selected" 
                                                                 delegate:nil 
                                                        cancelButtonTitle:@"OK" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:nil];

        [actionSheet showInView:[[self view]window]];
        [actionSheet autorelease];
    }

Upvotes: 22

Views: 25515

Answers (2)

MikeJfromVA
MikeJfromVA

Reputation: 513

It may be tricky to figure out if (UIImageView *)image has been initialized with an image. You may want to check the image property:

if (image.image == nil)

This will work in the case where image.image==nil (UIImageView initialized by image not set) AND in the case where image==nil (since messages sent to nil return nil).

Beware local variables as well: Just because you never alloc/init image doesn't mean it will be nil.

Upvotes: 7

Wilbur Vandrsmith
Wilbur Vandrsmith

Reputation: 5050

imageView.image == nil ? Or, to check for empty images, CGSizeEqualToSize(imageView.image.size, CGSizeZero).

Upvotes: 51

Related Questions