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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:18:33+00:00 2026-05-30T15:18:33+00:00

I want to generate buttons dynamically. The code below generates 2 buttons. But how

  • 0

I want to generate buttons dynamically. The code below generates 2 buttons. But how can I write a loop to generate lots (100 or 1000) buttons.

- (void)viewDidLoad
{
//allocate the view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

//set the view's background color
self.view.backgroundColor = [UIColor whiteColor];

//create the buttons
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
button1.frame = CGRectMake(200, 170, 100, 30);

//set the button's title
[button setTitle:@"Click Me!" forState:UIControlStateNormal];
[button1 setTitle:@"Click!" forState:UIControlStateNormal];

//listen for clicks
[button addTarget:self action:@selector(buttonPressed)
 forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self action:@selector(buttonPressed)
 forControlEvents:UIControlEventTouchUpInside];

//add the button to the view
[self.view addSubview:button];
[self.view addSubview:button1];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)buttonPressed {
NSLog(@"Button Pressed!");
}
  • 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-30T15:18:34+00:00Added an answer on May 30, 2026 at 3:18 pm

    I am actually stunned that you managed to pull off that code that you have there without knowing how to do a for loop.

    Beyond that, dont ever do this in viewDidLoad.

    //allocate the view
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    
    //set the view's background color
    self.view.backgroundColor = [UIColor whiteColor];
    

    The UIViewController loads its own view, you override it here for no real reason.

    -(void)viewDidLoad {
    
        [super viewDidLoad];
    
        for(int i = 0; i < 1000; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [button setFrame:CGRectMake(100 + i, 170 + i, 100, 30)];
    
            [button setTitle:@"Click Me!" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    
            [[self view] addSubview:button];
        }
    }
    
    -(void)buttonPressed {
        NSLog(@"Button Pressed!");
    }
    

    Note: please dont ever do this… I have no idea why you would want 1000 UIButtons, but there should be a much better approach to w/e you are trying to do.

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

Sidebar

Related Questions

How do I do if I want to programatically generate a set of buttons
I want to generate random colours which can be attractive in pie charts.I have
I've created some code that will dynamically generate a bunch of different LinkButtons. I've
Is it possible to generate buttons and outlets dynamically from e.g. data. I am
i want to iterate through a group of radio buttons generated dynamically and get
Let's say I have a class of 30 students and want generate every possible
I want to generate a thumbnail preview of videos in Java. I'm mostly JMF
I want to generate some XML in a stored procedure based on data in
I want to generate some formatted output of data retrieved from an MS-Access database
I want to generate a list in C#. I am missing python's list comprehensions.

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.