xcode 显示/隐藏标签(带切换按钮)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5164219/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:21:19  来源:igfitidea点击:

Show/Hide Label (w/ Toggle Button)

objective-cxcodemacosinterface-builder

提问by Zakman411

I have an application I'm working on where I need a button (upon click) to show/hide a label. I set up the label as an IBOutlet, and the button as an IBAction - but don't really know where to go from here. I'm still very new to cocoa - I figure it's pretty simple but objective-c is daunting to me. Any help (in dummies terms)?

我有一个正在处理的应用程序,我需要一个按钮(单击时)来显示/隐藏标签。我将标签设置为 IBOutlet,将按钮设置为 IBAction - 但真的不知道从哪里开始。我对可可还是很陌生 - 我认为它很简单,但 Objective-c 对我来说是令人生畏的。有什么帮助(用假人的话)?

回答by Nikita Rybak

Something like this should do.

这样的事情应该做。

if ([theLabel isHidden]) {
    [theLabel setHidden:NO];
} else {
    [theLabel setHidden:YES];
}

You can see the docs for both methods here.

您可以在此处查看这两种方法的文档。

回答by Redauser

There is another way to do that. You can simply use the "dot notation", it works this way:

还有另一种方法可以做到这一点。您可以简单地使用“点符号”,它是这样工作的:

if(theLabel.hidden == YES) {
     theLabel.hidden = NO;
}
else {
     theLabel.hidden = YES;
}

回答by dylan

Swift 3.0

斯威夫特 3.0

In the Swift Syntax, you can perform the show/hide button as follows, with an updating title string to identify it's state:

在 Swift 语法中,您可以按如下方式执行显示/隐藏按钮,并使用更新的标题字符串来标识其状态:

if (string.isHidden == true) {
   sender.title = "Hide"
    string.isHidden = false
} else {
    sender.title = "Show"
    string.isHidden = true
}

回答by Ed Norris

Or this:

或这个:

theLabel.hidden = !theLabel.hidden