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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:38:45+00:00 2026-05-30T16:38:45+00:00

this is my first post, any help would be greatly appreciated. I have to

  • 0

this is my first post, any help would be greatly appreciated.

I have to create a number of UIButtons programmatically, which I have done, but the code gets quite messy when there are alot of buttons so I have written a method that will take a UIbutton, string and UIcolor as input and set the rest of the UIButton attributes for me. The problem is the buttons don’t seem to be created. Is what I’m trying possible or am I going about it the wrong way.

The method

-(void)makeButton:(UIButton *)name titleOfButton:(NSString *)title buttonColor:(UIColor *)color {

    name =[[UIButton alloc] initWithFrame:(CGRectMake(400,400,150,100))];
    [name setAlpha:(0.5)];
    [name setBackgroundColor: color];
    [name setTitle:title forState:UIControlStateNormal];
    name.titleLabel.font = [UIFont systemFontOfSize:16];
    name.titleLabel. numberOfLines = 0; // Dynamic number of lines
    name.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    name.titleLabel.textColor = [UIColor blackColor];

    [myView addSubview:name];
}

and to call the method

[self makeButton:JDLabel
   titleOfButton:@"JD"
     buttonColor:[UIColor redColor]];

The method has been declared in the header file and the buttons at the top of the class.

  • 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-30T16:38:46+00:00Added an answer on May 30, 2026 at 4:38 pm

    This is some code to get you started. This will work if you paste it to your view controller, generally put it in the view you want but change “self.view” to “self” or any view you want as long as you have the reference to it. Just call “addSomeButtons” on “viewDidLoad” or as some target..

    - (void)buttonPressed:(UIButton *)sender {
        NSLog(@"button %d pressed", sender.tag+1);
    }
    - (void)makeButtonWithtitle:(NSString *)title andColor:(UIColor *)color atPositionIndex:(NSInteger)index {    
        const CGFloat buttonHeight = 60.0f;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(.0f, buttonHeight*index, 300.0f, buttonHeight)];    
    
        button.tag = index;
        [button setAlpha:(0.5)];
        [button setBackgroundColor: color];
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        button.titleLabel. numberOfLines = 0; // Dynamic number of lines
        button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        button.titleLabel.textColor = [UIColor blackColor];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [button release];
    }
    - (void)addSomeButtons {
        for(int i=0; i<10; i++) {
            [self makeButtonWithtitle:[NSString stringWithFormat:@"button %d", i+1] andColor:[UIColor greenColor] atPositionIndex:i];
        }
    }
    

    Anyway what you are trying to do is very common and very possible. For instance I never ever ever use the interface builder.

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

Sidebar

Related Questions

I am starting out and this is my first post. Any help will be
First post here. Hoping you guys can help. It would be much appreciated and
This is my first post and I my first experience with jquery. I have
This is my first post here. I have a problem. I need to take
this is my first post on StackOverflow, I have read a lot of pages
this is my first post here. I'm using an existing Flash widget but would
Hi, this is my first post! Hoping somebody might be able to help me
This is my first post in this forum, so please, be patient with me.
This is my first post on stackoverflow, so please excuse me if my question
This is my first post, thx, you've been very helpful. But I'm stuck. 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.