xcode 为标记隐藏以编程方式创建的 UIButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15214502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide programmatically created UIButton for tag
提问by Taylor Abernethy Newman
Currently I have 14 buttons being created programmatically using a for loop, code below:
目前,我使用 for 循环以编程方式创建了 14 个按钮,代码如下:
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];
}
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:
但是,在填充按钮后,我想将其从屏幕上删除。如果我调用 button.hidden 它只是隐藏以编程方式创建的最后一个按钮。我知道 button.tag 并尝试使用它,但感觉我几乎需要执行以下操作:
//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?
有没有类似的方法或方法可以做到这一点?
Update
更新
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,
我试图隐藏的按钮是以编程方式创建的。所以我希望 _buttonOne 具有创建按钮的标题(让我们称之为 letterButton),然后从视图中隐藏 letterButton,
UIButton *yourBtn = (UIButton *)[self.button viewWithTag:3];
[yourBtn setHidden:YES];
(code posted by Oh Seung Kwon)
This code work perfectly but it hides the wrong set of buttons. (Hides _buttonOne and not letterButton).
这段代码工作得很好,但它隐藏了错误的按钮组。(隐藏 _buttonOne 而不是 letterButton)。
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.
我想知道在 nib 中创建 12 个按钮并手动命名它们会不会更好……永远不会多于或少于 12 个。
回答by Jacob Relkin
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.
当您的按钮被点击时,您可以hidden
在 action 方法的sender
参数上设置属性,即实际被点击的按钮。这将隐藏被点击的按钮。
- (void)buttonSelected:(UIButton *)sender {
[_buttonOne setTitle:sender.titleLabel.text forState:UIControlStateNormal];
[sender setHidden:YES];
}
If you meant to retrieve the button with the tag of 3
, you can use this code instead:
如果您打算检索带有 标记的按钮,则3
可以改用以下代码:
[[self.view viewWithTag:3] setHidden:YES];
I don't recommend that you use the tag
property - you should use Interface Builder and an IBOutletCollection
instead.
我不建议您使用该tag
属性 - 您应该使用 Interface Builder 和 anIBOutletCollection
代替。
回答by Oh Seung Kwon
Like this
像这样
UIButton *yourBtn = (UIButton *)[self.view viewWithTag:3];
[yourBtn setHidden:YES];
回答by junkor
You could get the view by tag use this message.
您可以使用此消息按标签获取视图。
[self.view viewWithTag:3];
We always specific tag by macro just like
我们总是通过宏来具体标记,就像
#define kFirstButtonTag (100)
or use
或使用
#define kButtonBeginTag (100)
Then use the macro to get tag.
然后使用宏来获取标签。
And in a special number - Case 0, 1 or 2 is always use, begin your tag in a special number could avoid some problems
并且在特殊数字中 - 始终使用案例 0、1 或 2,以特殊数字开头您的标签可以避免一些问题