为标记隐藏以编程方式创建的 UIButton [英] Hide programmatically created UIButton for tag

查看:29
本文介绍了为标记隐藏以编程方式创建的 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用 for 循环以编程方式创建了 14 个按钮,代码如下:

Currently I have 14 buttons being created programmatically using a for loop, code below:

int buttonCount = 14;
for (int i=0; i< buttonCount; i++) {             
    
//Create titleString from array object
    NSString *stringFromInt = [NSString stringWithFormat:@"%@",[arrayForRound objectAtIndex:i]];
        

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [button addTarget:self
                   action:@selector(buttonSelected:)
         forControlEvents:UIControlEventTouchDown];

        [button setTitle:stringFromInt forState:UIControlStateNormal];

        button.titleLabel.font = [UIFont fontWithName:@"helvetica" size:19];
        button.tag = i;
        
        [self.view addSubview:button];
}

这非常适合创建按钮,然后我可以使用所选按钮的值填充答案框:

This works great to create the buttons, I can then populate the Answer Box with the value of the selected button:

-(void)buttonSelected: (UIButton *)sender
{
[_buttonOne setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}

但是,在填充按钮后,我想将其从屏幕上移除.如果我调用 button.hidden 它只是隐藏以编程方式创建的最后一个按钮.我知道 button.tag 并尝试使用它,但感觉我几乎需要做类似的事情:

However after the button has been populated I would like to remove it from the screen. If i call button.hidden it simply hides the last button that was programmatically created. I am aware of button.tag and have tried to use this, but it feels like I almost need to do something like:

//Hide button for tag (i know this is incorrect syntax)
button for buttonTag: 3 setHidden;

是否有类似的方法或方法?

Is there something similar or a way of doing this?

我试图隐藏的按钮是以编程方式创建的.所以我希望 _buttonOne 具有创建按钮的标题(让我们称之为 letterButton),然后从视图中隐藏 letterButton,

The button that I am trying to hide is the one that was created programatically. So I want _buttonOne to take on the title of the create button (lets call that letterButton), and then hide letterButton from the view,

UIButton *yourBtn = (UIButton *)[self.button viewWithTag:3];
[yourBtn setHidden:YES];
(code posted by Oh Seung Kwon)

这段代码运行良好,但它隐藏了错误的按钮组.(隐藏 _buttonOne 而不是 letterButton).

This code work perfectly but it hides the wrong set of buttons. (Hides _buttonOne and not letterButton).

我想知道在 nib 中创建 12 个按钮并手动命名它们会不会更好……永远不会多于或少于 12 个.

I wonder if it would not be better to create the 12 buttons in nib and name them manually...There will never be more or less that 12.

推荐答案

当你的按钮被点击时,你可以在操作方法的 sender 参数上设置 hidden 属性,这是实际被点击的按钮.这将隐藏被点击的按钮.

When your button is tapped, you can set the hidden property on the action method's sender argument, which is the button that actually got tapped. This will hide the button which was tapped.

- (void)buttonSelected:(UIButton *)sender {
   [_buttonOne setTitle:sender.titleLabel.text forState:UIControlStateNormal];
   [sender setHidden:YES];
}

如果您想检索带有 3 标签的按钮,您可以使用以下代码:

If you meant to retrieve the button with the tag of 3, you can use this code instead:

[[self.view viewWithTag:3] setHidden:YES];

我不建议您使用 tag 属性 - 您应该使用 Interface Builder 和 IBOutletCollection 代替.

I don't recommend that you use the tag property - you should use Interface Builder and an IBOutletCollection instead.

这篇关于为标记隐藏以编程方式创建的 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆