how to hide/show a button in swift
Asked Answered
S

4

34

I'm trying to have an if statement that will make a button hidden when a label displays a certain status, and appears when the label says something else. The name of the label is Status, and when it shows "Closed", I want it hidden, and when it shows "Open", it will appear.

var query3 = PFQuery(className:"Status_of_game")
query3.findObjectsInBackgroundWithBlock{

    (namelist3: [AnyObject]!, error : NSError!) -> Void in

    for list3 in namelist3 {

        var output = list3["StatusType"] as String

        self.Status.text = output

        println(output)

        if self.Status.text == "Closed" 
        {       
            Purchase().enable = false
        }
    }
}
Sligo answered 5/5, 2015 at 23:38 Comment(0)
G
38

As @LAmasse says, you want to use button.hidden = true. button.hidden was renamed to button.isHidden in Swift 3

The code you posted doesn't make sense.

if self.Status.text == "Closed" 
{
  Purchase().enable = false
}

What is Purchase? From the capitalized name, it seems to be a class. If so, the expression Purchase() is likely creating a new instance of the Purchase class, which makes no sense. Why are you making a function call? If that is creating a new Purchase object then that code is pointless. (You would create a new object inside the if statement that would be discarded on the very next line since you don't keep a strong reference to it.)

You want to set up an IBOutlet for your button and connect it in Interface Builder.

The declaration might look like this:

Class MyViewController: UIViewController
{
  @IBOutlet weak var theButton: UIButton!
  //The rest of your view controller's code goes here
}

If the outlet is connected to your button, there should be a filled-in circle to the left of the line of code. It looks like this:

enter image description here

And then your code to show/hide the button might look like this:

func showQueryResults
{
  var query3 = PFQuery(className:"Status_of_game")
  query3.findObjectsInBackgroundWithBlock()
  {
    (namelist3: [AnyObject]!, error : NSError!) -> Void in
    for list3 in namelist3 
    {
      var output = list3["StatusType"] as String
      self.Status.text = output
      println(output)
      if output == "Closed" 
      {
        theButton.isHidden = false //changed to isHidden for Swift 3
      }
    }
  }
}

It isn't clear to me why you'd loop though all of the results from your query and and show the button if the "StatusType" of any of the results is == "Closed".

Finally, I'm not very familiar with parse. If the completion block for the findObjectsInBackgroundWithBlock method doesn't get called on the main thread you will have to change that code to make the UI updates on the main thread.

EDIT:

I've since learned that Parse executes its completion handlers on the main thread, so you don't need to worry about UI calls from Parse completion handlers.

Glutamate answered 6/5, 2015 at 0:15 Comment(2)
awesome yea it worked I'm sorry i was putting it in the wrong section. Thank you both of you.Sligo
Note that the code above was written in Swift 2. I changed the one line that's under discussion to use the Swift 3 syntax theButton.isHidden, but have not updated the rest of the code to Swift 3.Glutamate
R
29

SWIFT 3

I created an

IBOutlet: loadingBDLogo

To Show:

loadingBDLogo.isHidden = false

To Hide:

self.loadingBDLogo.isHidden = true
Resume answered 9/1, 2017 at 7:3 Comment(1)
That's exactly what i am looking for (Swift 3)Ornithic
D
9

The sample code for hiding a button in Swift:

import UIKit

class ViewController: UIViewController {

// Create outlet for both the button
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    //Set button2 hidden at start
    button2.isHidden = true
}



//Here is the action when you press button1 which is visible
@IBAction func button1(sender: AnyObject) {
    //Make button2 Visible
    button2.isHidden = false
    }

}

And

You have to make the UIButton a property of the class if you want to keep a reference to it. Then you can access it using self.takePhotoButton.

Duer answered 6/5, 2015 at 4:59 Comment(0)
P
6

To hide a button, use button.hidden = true https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/cl/UIView

Placket answered 5/5, 2015 at 23:42 Comment(5)
I tried that to begin with because I remember obj-c being similar to that, but its still isn't working. if self.Status.text == "Closed" { Purchase().hidden = true }Sligo
Would you mind showing more code then? So that I can see this button to hide.Placket
thats all the code except for the connection for the button. Should the code for this if statement be in the brackets for the button? @IBAction func Purchase() { if Status == "Closed" { Purchase().hidden = true } }Sligo
Just hide it with an object : write "IBOutlet var button: UIButton!" Connect the button in your storyboard to "button" andthen use button.hidden = truePlacket
i know how to do that but i need it to work with the if statement so when i query the data from the server it switches the game off so know more people can join the game.Sligo

© 2022 - 2024 — McMap. All rights reserved.