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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:11:25+00:00 2026-05-21T19:11:25+00:00

This is a beginner’s question about the fundamentals of objective programming (at least I

  • 0

This is a beginner’s question about the fundamentals of objective programming (at least I think so..):

I have an UIView and would like to add several buttons to this UIView programatically. How many buttons there are depends on the user input.

Every button has four characteristics:

  1. Its number (e.g. button number one, two, three etc.)
  2. Its title (e.g. click this, click here, click me etc.)
  3. Its position and size (i.e. a CGRect – size stays the same, only y-position needs to change)
  4. Its colour (e.g. red, green, blue etc.)

Now I thought I can create a button by creating a method, but I am not entirely sure if this is a very sound method. I have the feeling that I should have created an object. Any help of how I should go about this would be very much appreciated. Here is my attempt – which is full of mistakes (for instance, I wasn’t sure of how to create button1, button2 etc. — hence the button[bNumber] thing):

 -(void) createIndexButtonWithNumber:(NSString *)bNumber withTitle:(NSString *)bTitle atPosition:(NSInteger *)bPosition withColour:(NSInteger *)bColour {

    CGRect buttonFrame = CGRectMake(0,bPosition,25,25);
    UIButton* button[bNumber] = [[[UIButton alloc] initWithFrame:buttonFrame] autorelease];
    UIButton.text = bTitle;
    if (bColour == 1) {UIButton.color = [UIColor colorWithRed:0.751 green:0.742 blue:0.715 alpha:1.000];};
    [indexView addSubview:button[bNumber]];

    // to do: if button[bNumber] pressed {do something};

}

Then, I would like to ‘multiply’ the object three times by calling:

for (temp = 0; temp < 2; temp++) {
[self createIndexButtonWithNumber:temp withTitle:[@"Test%i", temp] atPosition:10 withColour:1];}

I’m sure this is all a bit problematic, wrong and clumsy, so I’d be very grateful for any suggestions of how to tackle this.

  • 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-21T19:11:25+00:00Added an answer on May 21, 2026 at 7:11 pm

    I’d like to reference to UIButton Class Reference

    1. You should create buttons only with buttonWithType: method.

    2. Second, there is no button color. You can set a color for title with setTitleColor:forState: method.

    3. There are just syntax mistakes in your code (UIButton.text, etc). You cant do such a call. UIButton is a class.

    4. Your calling createIndexButtonWithNumber... within for will place buttons in the same place — atPosition input parameter has no change.

    5. NSInteger variable is not a pointer — * is not needed.

    6. …

    In the end of struggling we have smth like that:

    -(void)createIndexButtonWithNumber:(NSInteger)bNumber //(sorry for that, some troubles with code formatting)

                              withTitle:(NSString *)bTitle 
                             atPosition:(NSInteger)bPosition 
                             withColour:(NSInteger)bColour {
        CGRect buttonFrame = CGRectMake(0,bPosition,25,25);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
        button.frame = buttonFrame;
        button.tag = bNumber;
        [button setTitle: [NSString stringWithFormat:@"Button %i", bNumber]  
                forState:UIControlStateNormal];
        if (bColour == 1) {
            [button setTitleColor:[UIColor colorWithRed:0.751 green:0.742 blue:0.715 alpha:1.000]
                         forState:UIControlStateNormal];
        };
    
        // you should pass to @selector a description like 'indexAction:' but NOT any calls like '[self indexAction:button]'
        [button addTarget:self
                   action:@selector(indexAction:)
         forControlEvents:UIControlEventTouchUpInside];
        [indexView addSubview:button];
    }
    

    And for call:

    for (int temp = 0; temp < 2; temp++) {
        [self createIndexButtonWithNumber:temp withTitle:[@"Test%i", temp] atPosition:30 * temp withColour:1];
    }
    

    UPDATE: Selector method

    -(void)indexAction:(id)sender {
        NSLog(@"Button with tag %d is pressed", ((UIButton *)sender).tag);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner in this very confusing Objective-C game. I would like to
This is a beginner level question for asp.net MVC I have the following code
This may sound like a stupid question but I'm a beginner not just to
This is a beginner-level question. I have a catalog of mtypes: mtype_id name 1
i know this question was asked before but i have this very weird beginner
Hi this is ROR beginner's question. I have creat controller.rb and view hello.rhtml following
Ok, this is a beginner's question. I have a main page with a header
This is a beginner question, but please bear with me. I'd like to pass
Ok I have another question and I'm a beginner at this. I have this
This is a beginner question, I have to create some SQL Reports with business

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.