Is there a way, using the Interface Builder, in XCode to display a random image? So let’s say I have three images and want a random one to display each time my app is loaded. I’m new to XCode, so I’m looking for the simplest way to do it and IB seems to be the route to take. Thanks!
Share
After thinking about this for a few seconds, I realized the answer is of course you can, but it is such a pain, that I doubt that you would want to. I came up with a approach as an example of how to use IB in such a way.
First, subclass UIImageView, I will call it MyRandomImageView. The only member of MyRandomImageView is
@property (retain, nonatomic) NSString *randomImageJSON.Next, add a UIImageView to your view. In the Identity Inspector, change the Custom Class from UIImageView to MyRandomImageView. Then in the Identity Inspector, under User Defined Runtime Attributes, set randomImageJSON to something like
[ "image1.png", "image2.png", "image3.png" ].The final part is where you have all the trouble. Create
-[MyRandomImageView setRandomImageJSON]. It needs to set the_randomImageJSONiVar, useNSJSONSerializationto create the array of strings, randomly pick one of the strings, then setself.image = [UIImage imageNamed:randomImage]This should do exactly and you wish, a random image from the Interface Builder. Hope that helps.