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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:55:48+00:00 2026-05-22T20:55:48+00:00

I have created buttons are programmatically and set the button tags. View Did Load:

  • 0

I have created buttons are programmatically and set the button tags.

View Did Load:

I have created one view and added the buttons are sub view into that view.

-(void) viewDidLoad
{
   self.answerSubview = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 230)];

   self.answerSubview.backgroundColor = [UIColor clearColor];

   [self.view addSubview:answerSubview];

   [self buttonCreation];
}

-(void) buttonCreation{ 
int x =100;

for(i = 0; i < 4; i++) {

      UIButton *answerBtn  = [UIButton buttonWithType:UIButtonTypeRoundedRect];

      [answerBtn setFrame:CGRectMake(40, x, 260, 40)];

      answerBtn.tag = ar[i];    

      [answerBtn setTitle:[answerList objectAtIndex:i] forState:UIControlStateNormal];

      [self.answerSubview addSubview:answerBtn];

      x = x+50;
}
}

And i want to set the background image for the certain index of the button, when the time is finished,

 -(void) timerFinished
 {
         for (UIView* view in [self.answerSubview subviews]) { 

    if([view isKindOfClass:[UIButton class]]){ 

         UIButton *btn = (UIButton*)view; 

     NSLog(@"button value %d", btn.tag); 

         if(btn.tag == correctIndex)
        {
         [btn setBackgroundImage:[UIImage imageNamed:@"selected_correct_answer.png"] forState:UIControlStateNormal]; 

         }
         } 
    }

  }

But every time,the tag values are added into the stack,I have randomly generated numbers and set it into the button tags, so now it comes like,
REsult:

First time, Second time,

  0             0
  1             1
  3             3
  2             2 
                3
                1
                2
                0  

(Actually my expected result is 3 1 2 0).

So how could i remove the previous tag values and get the correct tag values at a time.

Please help me out.

  • 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-22T20:55:49+00:00Added an answer on May 22, 2026 at 8:55 pm

    There are two things you can consider doing. Since the only thing that changes on every iteration is the text on the button. You could probably reuse them. Setup once and put them in ivars. On every iteration, change the text and tag of the button.

    If that is not the route you’re interested in, you can probably send removeFromSuperview message to those four button objects.

    However, the first method is a better approach in my opinion because it will do away with continuous creation-destruction cycle.

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

Sidebar

Related Questions

I have created 10 buttons programmatically and set the titles in the button. Now
I have to create a button programmatically, and set an image background at that
I have created linearlayout with layout:height=fill_parent I have set 3 buttons and one TextView.
I have a button that is programatically created, it's content is a stack panel
In Cocoa/Objective-C if I have created a button programmatically, what do I put in
I have created a grouped table view & I had a edit button in
I have created two buttons using the following XAML code. <Button x:Name=Button1 Width=100 Content=Button1
I have created an item swapper control consisting in two listboxes and some buttons
I have a button on a website that creates a directory and copys a
This is a weird issue. I have created a view controller with a nib

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.