开发者

Is it bad idea to add nil as a subview?

Basically, I've created a custom view (extending UIView) that displays at least one image (but sometimes two, one on top of each other). The bottom image is a placeholder and since the image being placed on it开发者_Go百科 can be deleted, I would like to be able to just remove the top image so the placeholder is visible again. I've created a message for placing an image on top of the placeholder (it removes and releases the last subview and adds the new one) and a message for showing the placeholder. During the course of this message [self addSubview: nil] will be called.

My questions are:

  • Will addSubview: nil cause UB? Is it a bad idea?
  • After adding nil as a subview, will it be added to the subviews array or will it be ignored?


Is it really a good idea to add two image views to show the placeholder image and original image?

Just ask yourself why do you want to have two image views separately to show the placeholder and the original image? You can have just one image view and show the placeholder image if the original image is not available meanwhile show the original image if it is available.

- (void)setMainImage:(UIImage *)mainImage {

    if (mainImage) self.mainImageView.image = mainImage;
    else self.mainImageView.image = thePlaceholderImage;
}

If you want to remove the main image, just call

[self setMainImage:nil];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜