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 6552517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:28:08+00:00 2026-05-25T12:28:08+00:00

I have 10 images in array and 10 buttons, all i want is to

  • 0

I have 10 images in array and 10 buttons, all i want is to set the images to buttons randomly.

i tried this but not succeeded

int index = arc4random() % 10;

UIImage *img = [UIImage imageNamed:[arrayAnsewrImages objectAtIndex:index]];

[self.btnAnswerImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];

here is the code of method , m using :

[self.arrayQuestionImages addObject:@"bear.jpg"];
[self.arrayQuestionImages addObject:@"girraf.jpg"];
[self.arrayQuestionImages addObject:@"hippo.jpg"];
[self.arrayQuestionImages addObject:@"monkey.jpg"];
[self.arrayQuestionImages addObject:@"piglet.jpg"];
[self.arrayQuestionImages addObject:@"pumba.jpg"];
[self.arrayQuestionImages addObject:@"rabbit.jpg"];
[self.arrayQuestionImages addObject:@"simba.jpg"];
[self.arrayQuestionImages addObject:@"snake.jpg"];
[self.arrayQuestionImages addObject:@"tigger.jpg"];
[self.arrayQuestionImages addObject:@"turtle.jpg"];
int index = arc4random() % 10;

UIImage *img = [UIImage imageNamed:[arrayQuestionImages objectAtIndex:index]];


switch (index)
{   

    case 0:
    {   
        //[self.btnQuestionImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
        [self.btnAnswerImage1 setImage:img forState:UIControlStateNormal];

    }
        break;
    case 1:
    {
        [self.btnQuestionImage2 setImage:img forState:UIControlStateNormal];    
    }               
        break;
    case 2:
    {
        [self.btnQuestionImage3 setImage:img forState:UIControlStateNormal];    

    }   
        break;
    case 3:
    {
        [self.btnQuestionImage4 setImage:img forState:UIControlStateNormal];

    }
        break;
    case 4:
    {
        [self.btnQuestionImage5 setImage:img forState:UIControlStateNormal];    

    }   
        break;  
    case 5:
    {
        [self.btnQuestionImage6 setImage:img forState:UIControlStateNormal];    

    }   
        break;  
    case 6:
    {
        [self.btnQuestionImage7 setImage:img forState:UIControlStateNormal];    

    }   
        break;  
    case 7:
    {
        [self.btnQuestionImage8 setImage:img forState:UIControlStateNormal];

    }   
        break;  
    case 8:
    {
        [self.btnQuestionImage9 setImage:img forState:UIControlStateNormal];    

    }   
        break;  
    case 9:
    {
        [self.btnQuestionImage10 setImage:img forState:UIControlStateNormal];   

    }   
        break;  



    default:
    {
        [self.btnQuestionImage1 setImage:img forState:UIControlStateNormal];

    }
        break;
} 
}

M Getting Blank/nil image on my every button. Please suggest me any option.

  • 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-25T12:28:09+00:00Added an answer on May 25, 2026 at 12:28 pm

    Nothing strikes me as being that odd in your code, but there are a couple of little things I’d check…

    Your call to [UIImage imageNamed:] needs to take a string as a parameter, yet it appears (at least by the name of your array) that you are passing in a UIImage object. The code should be one of the following:

    UIImage *img = [UIImage imageNamed:[arrayAnswerImageNames objectAtIndex:index]];  // Note "Names" array
    

    or:

    UIImage *img = (UIImage *)[arrayAnswerImages objectAtIndex:index];
    

    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?

    Hope you get it sorted.

    EDIT

    Just looked at your additional code. Just a thought – have you initialised your array arrayQuestionImages? Make sure you call something similar to:

    self.arrayQuestionImages = [NSMutableArray arrayWithCapacity:10];
    

    prior to adding the objects.

    Also, using a switch statement in the way you have shown above looks a bit obfuscated. Personally, I would do something like the following:

    - (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 < 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.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried all the combinations I can think off but this form: class
I have an images array in which there are number of images. Not fixed,
i have two int array of images , i have implemented it in grid
So I have this task to create a horizontal scrolling array of image buttons
I want to declare a global array so I can use this in all
I need to get array of images that have to call from server and
I need to display array of images in a single view which have various
I have image Array with two images out of that first image its showing
I have an array of urls of images, from which i have to first
I have an observable array with var items= [ {image: /images/image1.jpg }, {image: /images/image2.jpg

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.