(iPhone SDK)如何通过标签ID修改UIButton?

时间:2011-02-17 17:38:40

标签: iphone uibutton

我创建了一组按钮,如下所示:

NSString *nameImg;
UIButton *button;

for(int i=1;i<=144;i++){
    button = [[UIButton alloc] initWithFrame:CGRectMake(posX, posY, 43, 43)];
    nameImg = [NSString stringWithFormat:@"img%i.png",i];
    [button setBackgroundImage:[UIImage imageNamed:nameImg] forState:(UIControlState)normal];
    button.tag = i;
    [scrollView addSubview:button];

    posX += 55;

    if (i % 4 == 0){
            posY += 55;
        posX = 15;
    }
}

如何修改按标签获取的按钮?可以做类似以下的事情吗?

[button.tagId setTitle:@"hello"];

我不想要一个监听器方法,我只想修改按标签识别它的按钮。

提前致谢

3 个答案:

答案 0 :(得分:9)

[(UIButton*)[scrollView viewWithTag:tagId] setTitle:@"hello" forState:UIControlStateNormal];

答案 1 :(得分:5)

在父视图上调用viewWithTag:

UIButton *btn = (UIButton *)[scrollView viewWithTag:tagID];

然后你可以使用btn进行任何更改。

在问题中没有问到,你正在为循环中的按钮泄漏内存。将此添加到scrollView后,您需要将release发送到按钮。

答案 2 :(得分:0)