开发者

Show/Hide Label (w/ Toggle Button)

开发者 https://www.devze.com 2023-02-14 19:17 出处:网络
I have an ap开发者_高级运维plication 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

I have an ap开发者_高级运维plication 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)?


Something like this should do.

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

You can see the docs for both methods here.


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


Swift 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:

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


Or this:

theLabel.hidden = !theLabel.hidden
0

精彩评论

暂无评论...
验证码 换一张
取 消