I have an array of UIButtons. What I want to do is with another button, randomly set the positions of each of the buttons in the array.
So I initialize the array with the UIButtons:
buttonArray = [[NSMutableArray alloc] initWithObjects:button1,button2,button3,button4,button5,button6,button7, nil];
Then I have a randomize method to set each of the button’s positions.
this is the part where I’m stuck. I’ve found some threads about having to cast the type of the object in the array so the compiler understands. but I can’t seem to make it work.
- (IBAction)randomizePositions:(id)sender
{
for (int i = 0; i < [buttonArray count]; ++i)
{
float xPos = arc4random() % 1000;
float yPos = arc4random() % 700;
CGRect randomPosition = CGRectMake(xPos, yPos, button1.frame.size.width, button1.frame.size.width);
(UIButton *)[buttonArray objectAtIndex:i].frame = randomPosition;
}
}
It’s this part here that I can’t seem to get right. It’s pretty obvious by now that I’m a beginner so any help would be much appreaciated.
(UIButton *)[buttonArray objectAtIndex:i].frame = randomPosition;
You may want to grab a pointer to the UIButton in the array earlier, as it might be easier to consider what you are working with.
Unless, of course, you want to always be working with button1 size.