Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nothing strikes me as being <em>that</em> odd in your code, but there are a couple of little things I'd check...</p> <p>Your call to <code>[UIImage imageNamed:]</code> needs to take a string as a parameter, yet it <em>appears</em> (at least by the name of your array) that you are passing in a UIImage object. The code should be one of the following:</p> <pre><code>UIImage *img = [UIImage imageNamed:[arrayAnswerImageNames objectAtIndex:index]]; // Note "Names" array </code></pre> <p>or:</p> <pre><code>UIImage *img = (UIImage *)[arrayAnswerImages objectAtIndex:index]; </code></pre> <p>depending on how you want to build your 'images' array; with images, or imageNames. Also, I presume the frame of your buttons are set, and that they are added to a view?</p> <p>Hope you get it sorted.</p> <p><br> <strong>EDIT</strong></p> <p>Just looked at your additional code. Just a thought - have you initialised your array <code>arrayQuestionImages</code>? Make sure you call something similar to:</p> <pre><code>self.arrayQuestionImages = [NSMutableArray arrayWithCapacity:10]; </code></pre> <p>prior to adding the objects.</p> <p>Also, using a switch statement in the way you have shown above looks a bit obfuscated. Personally, I would do something like the following:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; self.questionImageNames = [NSArray arrayWithObjects:@"bear.jpg", @"girraf.jpg", @"hippo.jpg", @"monkey.jpg", @"piglet.jpg", @"pumba.jpg", @"rabbit.jpg", @"simba.jpg", @"snake.jpg", @"tigger.jpg", @"turtle.jpg", nil]; NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:10]; for (int i = 0; i &lt; NUM_BUTTONS; i++) { NSUInteger index = arc4random() % NUM_BUTTONS; UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom]; newButton.frame = CGRectMake(i * x, i * y, width, height); // Whatever position and size you need... UIImage *buttonImage = [UIImage imageNamed:[self.questionImageNames objectAtIndex:index]]; [newButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; [self.view addSubview:newButton]; [buttonArray addObject:newButton]; } self.myButtons = buttonArray; // Where myButtons is a NSArray property of your class, to store references to the buttons. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload