how to hide/show a button in swift

SWIFT 3

I created an

IBOutlet: loadingBDLogo

To Show:

loadingBDLogo.isHidden = false

To Hide:

self.loadingBDLogo.isHidden = true

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


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.


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.