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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:02:09+00:00 2026-06-11T06:02:09+00:00

I Follow the comment and try to fix my problem,but still not working. when

  • 0

I Follow the comment and try to fix my problem,but still not working.

when i run the test demo on the simulator,i get this:

run the demo project view

and i click the test2, i want to change button title before clear the button title, but i

get this :

after i click test2 button

i can not clear the button title when click another button.

anyone can help ??

Here is my code

-(void)addbutton
{
    for (int i=0; i<3; i++)
    {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((i*100), 0, 100, 100)];
        button.titleLabel.text = @"";
        [button setTag:i];
        [button setTitle:[self.demoDataArray objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        [button addTarget:self action:@selector(setButtonTitle:) forControlEvents:UIControlEventTouchUpInside];
        [self.demoView addSubview:button];
    }
}

-(IBAction)setButtonTitle:(id)sender
{
    if ([sender tag] == 0)
    {
        self.demoDataArray = [[NSArray alloc] initWithObjects:@"test5", @"test6", @"test7", @"test8", nil];
        [self addbutton];
    }
    else if([sender tag] == 1)
    {
        self.demoDataArray = [[NSArray alloc] initWithObjects:@"test9", @"test10", @"test11", @"test12", nil];
        [self addbutton];
    }
    else if([sender tag] == 3)
    {
        self.demoDataArray = [[NSArray alloc]initWithObjects:@"test13", @"test14", @"test15", @"test16", nil];
        [self addbutton];
    }
}
  • 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-06-11T06:02:11+00:00Added an answer on June 11, 2026 at 6:02 am

    Every time any button is pressed, you instantiate new buttons and add them on top of the other ones. You probably want to just update the existing buttons. Try this for your -addButton method instead:

    -(void)addbutton
    {
        for (int i=0;i<3;i++)
        {
            NSInteger tag = i+1;
            UIButton *button = (UIButton *)[self.view viewWithTag:tag];
            if (!button)
            {
                button = [[UIButton alloc] initWithFrame:CGRectMake((i*100), 0, 100, 100)];
                [button addTarget:self action:@selector(setButtonTitle:) forControlEvents:UIControlEventTouchUpInside];
                [button setTag:tag];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                button.backgroundColor = [UIColor clearColor];
                [self.demoView addSubview:button];
            }
    
            button.titleLabel.text = @""; // Not actually sure you need this...
            NSString *titleText = nil;
            if (i < self.demoDataArray.count)
                titleText = [self.demoDataArray objectAtIndex:i];
            [button setTitle:titleText forState:UIControlStateNormal];
    
        }
    }
    

    Now the button is instantiated, given a target and action, tagged, and added into the button hierarchy only when it doesn’t already exist. Every time a button is tapped after that, only the titles will be updated.

    Also, very important:: in order for -viewWithTag: to work, you have to use a tag for your buttons that is not equal to 0, the default tag – otherwise the button’s superview will be returned. This means you’ll need to make the following changes in your button handler, which already had a bug with checking the tags (checking against 3 rather than 2). Increment the tags to start at 1 and end with 3 like so:

    -(IBAction)setButtonTitle:(id)sender
    {
        if ([sender tag] == 1)
        {
            self.demoDataArray = [[NSArray alloc] initWithObjects:@"test5",@"test6",@"test7",@"test8", nil];
            [self addbutton];
        }
        else if([sender tag] == 2)
        {
            self.demoDataArray = [[NSArray alloc] initWithObjects:@"test9",@"test10",@"test11",@"test12", nil];
            [self addbutton];
        }
        else if([sender tag] == 3)
        {
            self.demoDataArray = [[NSArray alloc]initWithObjects:@"test13",@"test14",@"test15",@"test16", nil];
            [self addbutton];
        }
    }
    

    That method could use some cleanup as well to eliminate duplicated code and it’s probably more appropriate to use a switch statement here anyways. But that’s neither here nor there.

    OK, this should probably get you up and running. I didn’t test or compile this so if you do have some problem with the compiler that you’re not able to work through on your own please let me know and I’ll see if I can spot the issue.

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

Sidebar

Related Questions

I try to follow Akrabats Tutorial the Application/Index is working, the Album part not.
I follow this tutorial online exactly but somehow it's giving me errors. Saying there
This almost seems silly but what is the most reliable pattern to follow when
Im not sure if i have explaned this the best i can but, here
Follow up to this question . I have the following code: string[] names =
Follow up to this question about GNU make : I've got a directory, flac
Follow up to this question for Facebook Friends.getAppUsers using Graph API that pulls friends
I follow example from here using section listview http://lalit3686.blogspot.com/2012/05/sectionadapter.html But how can I implement
There are some other variations of this question here at SO, but please read
This in one of the thing that still confuses me using Marionette. Imagine a

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.