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

  • Home
  • SEARCH
  • 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 8944733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:05:23+00:00 2026-06-15T12:05:23+00:00

I have several UIButtons created from an NSMutableArray. I’m trying to customize each UIButton

  • 0

I have several UIButtons created from an NSMutableArray. I’m trying to customize each UIButton with a unique image.

On the following line I’m not sure of the syntax to use to set an image for each button from the image array respectively:

[btn setImage:[UIImage imageNamed:[myImages objectAtIndex:3]] forState:UIControlStateNormal];

here’s the rest of my code:

NSMutableArray* buttonArray = [[NSMutableArray alloc] init];

for(int i = 0; i < 8; i++)
{
    // Custom UIButton
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setFrame:CGRectMake(0.0f, 2.0f, 52.0f, 52.0f)];
    [btn setTitle:[NSString stringWithFormat:@"Button %d", i+1] forState:UIControlStateNormal];

    NSArray * myImages = [NSArray arrayWithObjects:@"category0.png", @"category-clothing1.png" , @"category2.png", @"category3.png", nil];

    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:3]] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [buttonArray addObject:btn];

}

I would also like a different selector to be called for each button.

  • 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-06-15T12:05:24+00:00Added an answer on June 15, 2026 at 12:05 pm

    Something like this? I just saw your post about needing different actions for each button but I would instead check the sender of the button and adjust the code accordingly inside your (buttonPressed:) method. You should also make sure you have the appropriate amount of images for your button array so a better method may be to only create the amount of buttons based on your image array count. (answer edited to reflect this). I also wanted to add you should get in the habit of also changing the highlighted state of the image when changing the normal state so when you press the image it doesn’t go blank.

    NSMutableArray* buttonArray = [NSMutableArray array];
    NSArray * myImages = [NSArray arrayWithObjects:@"category0.png", @"category-clothing1.png" , @"category2.png", @"category3.png", nil];
    
    for(int i = 0;i < [myImages count]; i++)
    {
        // Custom UIButton
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
        [btn setFrame:CGRectMake(0.0f, 2.0f, 52.0f, 52.0f)];
        [btn setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateHighlighted];
        [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [buttonArray addObject:btn];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom UITableViewCell which contains several UIButtons. Each button's frame position is
we have several tens of macro enabled excel files, each of those contains few
I have Several UIImageViews in a NSMutableArray. They are all in the superView. I
I have several CRUD operations to perform, each one on a collection of models
I have several UIViews and UIImageViews that are loaded inside a UIScrollView. Each page,
I have the following problem. On a UIScrollView are laid out several components :
I have several classes that I serialize/deserialize, each with a number of properties, some
I have several hosts from which i want to do the same query. So
I have several objects, like products, order, etc. When I get information from my
I have several vehicles that send a data to server each minute. The server

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.