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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:15:51+00:00 2026-05-21T09:15:51+00:00

I have 39 different UIButton variables in my .h file, but I would like

  • 0

I have 39 different UIButton variables in my .h file, but I would like to add each of them to an array without having to type out the same thing 39 times.

Is there a way that I could do this in a for loop?

The buttons are named accordingly: btn1,btn2,btn3 etc.

  • 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-21T09:15:51+00:00Added an answer on May 21, 2026 at 9:15 am

    You might want to forego the 39 buttons in your header file and instead have a single array.
    I suspect that you want to use manual references so you can take advantage of Interface Builder, to control events and layout. I suggest doing something a little different.

    Create a single property – an NSMutableArray. Then, when the view loads, create the buttons on the fly.

    To access a button, use something like [self.arrayOfButtons objectAtIndex:38];. (In the case of 39 buttons, that would return the last button.);`

    To create a button, you use the following:

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    

    Note that you pass in the frame of your button’s init method. The frame of your button is going to start in the top left corner of its container and your button will be 100 pixels square. The frame is an instance of CGRect. (You create a CGRect by calling the function CGRectMake(x,y,width,height).

    To make 39 buttons, you might want to loop as follows, given an array to hold the buttons, myButtons and predefined numberOfButtons and dimension variables:

    for(int i=0;i<numberOfButtons;i++){
      //Create the button
      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width, height)];
      //Store the button in our array
      [self.myArray addObject:button];
      //Release the button (our array retains it for us)
      [button release];
    }
    

    Of course, you are going need to set unique values for x,y,width and height for each button or they will all overlap. Once you’ve created your buttons, you can do things with them, like set the label, or show them onscreen. To show them onscreen, you can do something like this:

    for(UIButton *button in self.myButtons){
      //add the button to the view
      [self.view addSubview:button];
    }
    

    Of course, just adding buttons to the screen is useless. You need to be able to make them do something. To add an action to a button, you can use:

    [button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
    

    The first part, addTarget:self, says that this view controller handles the event that you’re going to ask it to handle. action:@selector(someMethod:) tells the class what method to perform when the event occurs. Then, forControlEvents:UIControlEventTouchDown says that the said class should perform the said method when the button is tapped.

    So, in your header:

    #import <UIKit/UIKit.h>
    
    @interface MyClass :UIViewController{
    
       NSMutableArray *myButtons; 
    }
    
    @property (nonatomic, retain) NSMutableArray *myButtons;
    
    //Use UIButton as the sender type if you want 
    - (void)someMethod:(id)sender;
    
    // ... Other code can go here of course
    @end
    

    And in your implementation, you can use this:

    #import MyClass.h
    
    
    @implementation MyClass
    
    - (id)init{
    
      self = [super init];
    
      if(self){
    
         NSMutableArray *temp = [[NSMutableArray alloc] init];
         self.myButtons = temp;
         [temp release];
    
      }
    
      return self;
    
    }
    
    
    - (void)viewDidLoad{
    
      //
      // Create the buttons
      //
    
      for(int i=0;i<numberOfButtons;i++){
        //Create the button
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width, height)];
    
         //You may want to set button titles here
         //or perform other customizations
    
        //Store the button in our array
        [self.myArray addObject:button];
        //Release the button (our array retains it for us)
        [button release];
      }  
    
      //
      // Show the buttons onscreen
      //
    
      for(UIButton *button in self.myButtons){
        //add the button to the view
        [self.view addSubview:button];
        //add the action
        [button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDown];
      }
    }
    
    - (void)someMethod:(id)sender{
       //Do something when the button was pressed;
    }
    
    - (void)dealloc{
      [self.myButtons release];
      [super dealloc];
    }
    
    @end
    

    Now, you can go to button paradise without taking Interface Builder on the plane. Go UIButton happy!

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

Sidebar

Related Questions

Hey! I have a xib file were i would like a round rect button
I have an AVURLAsset with multiple AVAssetTracks of type audio. I would like to
I have two functions that have different enough logic but pretty much the same
I want to accomplish touching a UIButton and having code run in a different
In my nib file, I have an outlet connected a UIButton for the UITableViewCell.
Hello guys i have cells in tableview, but there are two different UIButtons to
I would like to create a UIButton which uses a stretchable image as Background
Simple question: If I have a UIButton and fill it with different subviews (UIViews,
I have written this code to see different image states... UIButton *btnComment = [UIButton
I have created a custom UIView that I would like to use on multiple

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.