清除uibutton标题并重新重置

时间:2012-09-04 08:58:58

标签: iphone uibutton

我关注评论并尝试解决我的问题,但仍无法正常工作。

当我在模拟器上运行测试演示时,我得到了这个:

run the demo project view

然后我点击test2,我想在清除按钮标题之前更改按钮标题,但我

得到这个:

after i click test2 button

单击另一个按钮时无法清除按钮标题。

谁能帮忙?

这是我的代码

-(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];
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个

- (IBAction)firstbtn:(UIButton *)sender {

  [secondbtn setTitle:@"" forState:UIControlStateNormal];

   }

答案 1 :(得分:1)

每次按下任何按钮,您都会实例化新按钮并将其添加到其他按钮之上。您可能只想更新现有按钮。请尝试使用-addButton方法:

-(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];

    }
}

现在按钮被实例化,给定目标和动作,标记,并且只有当它不存在时才被添加到按钮层次结构中。每次点击按钮后,只会更新标题。

另外,非常重要::为了使-viewWithTag:能够正常工作,您必须使用不等于0的按钮标签,默认标签 - 否则按钮& #39; s superview将被退回。这意味着您需要在按钮处理程序中进行以下更改,这已经有检查标记的错误(检查3而不是2)。增加标签从1开始,以3结束,如下所示:

-(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];
    }
}

该方法也可以使用一些清理来消除重复的代码,而且在这里使用switch语句可能更合适。但那既不在这里,也不在那里。

好的,这可能会让你起步并运行。我没有测试或编译这个,所以如果你对编译器有一些问题,你不能自己解决,请告诉我,我会看看我是否能发现问题。