Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle hidden state of UILabel

Tags:

ios

swift

I want to use the press of a button to toggle the hidden state of a couple of UILabels. Press the button once, it unhides them, press again the labels are hidden, the default state for these labels being hidden.

Here is what I thought would work (but obviously does not):

@IBAction func information(sender: AnyObject, forEvent event: UIEvent)
{
    if(infoLocation.hidden = true)
    {
        self.infoLocation.hidden = false
    }
    else
    {
        self.infoLocation.hidden = true
    }

    //**********************************//
    if(infoName.hidden = true)
    {
        self.infoName.hidden = false
    }
    else
    {
        self.infoName.hidden = true
    }

    //**********************************//
    if(infoVersion.hidden = true)
    {
        self.infoVersion.hidden = false
    }
    else
    {
        self.infoVersion.hidden = true
    }


}
like image 848
MicrosoftDave Avatar asked May 04 '15 17:05

MicrosoftDave


People also ask

How do I toggle between the current state and hidden state?

Use one trigger to quickly toggle between an object's current state and its hidden or disabled state. For example, easily create buttons that show/hide additional info or hints. Check out the comparison below: In the trigger wizard, select the Change state of action.

Why is the label no longer being hidden in my form?

The label no longer being hidden is impacted by the Summer '18 change where many base lightning components no longer automatically include the slds-form_inline or slds-form--inline CSS class. The slds-form_inline (formerly slds-form--inline) style was removed. When applied, the style made the element’s width unchangeable.

How do I toggle (hide/show) an element?

Toggle Hide and Show Click the button! Toggle (Hide/Show) an Element Step 1) Add HTML: Example <button onclick="myFunction()">Click Me</button>

How do I toggle hidden/disabled state triggers in storyline 360?

Choose either the Hidden or the Disabled state and set up your trigger event (e.g., when a button is clicked). Toggle hidden/disabled state triggers are exclusive to Storyline 360 starting with the July 2022 update. Project files that use this feature won't open in previous versions of Storyline 360 and Storyline 3.


2 Answers

To make the code shorter (and look better) I would do like this if its just a toggle:

for label in [label1, label2, label3, label4] {
    label.hidden = !label.hidden
}

This will toggle label1-4.hidden

like image 151
Arbitur Avatar answered Sep 23 '22 02:09

Arbitur


Use == inside of your if statement, not =.

if(infoVersion.hidden == true)

= is for assignment.
== is for equality.

update :
You can use a faster version code to toogle hidden status :

infoVersion.hidden = !infoVersion.hidden;
like image 22
David 'mArm' Ansermot Avatar answered Sep 24 '22 02:09

David 'mArm' Ansermot