Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7493507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:00:00+00:00 2026-05-29T17:00:00+00:00

I have an array of UIButtons . What I want to do is with

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:00:02+00:00Added an answer on May 29, 2026 at 5:00 pm

    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.

    - (IBAction)randomizePositions:(id)sender 
    {
        for (int i = 0; i < [buttonArray count]; ++i) 
        {
            UIButton *currentButton = (UIButton *)[buttonArray objectAtIndex:i];
            float xPos = arc4random() % 1000;
            float yPos = arc4random() % 700;
            [currentButton setFrame:CGRectMake(xPos, yPos, currentButton.frame.size.width, currentButton.frame.size.height)];
        }
    }
    

    Unless, of course, you want to always be working with button1 size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Two NSMutableArray .My First Array contain 3 UIButtons along With Buttons Titles
I have a UIButton array. UIButton *btn[12]; I want to set property for this
Basically I want to create 48 button outlets. I have created the buttons in
I want to implement UITableView Where I want to have 3 buttons in each
I want to edit all buttons with a specific tag. I have tagged each
I have array in following format. I want to convert it into another format
I have 10 UIimages and 10 UIButtons. M assigning Images to buttons via array.
I'm going to have a 10x10 grid of UIButton objects. Each of these UIButtons
I have an Array with 26 Alphabets and Second Array with 3 UIButtons. I
I have an NSMutableArray with 3 UIButtons and Second NSMutableArray with 50 UIButtons. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.