how to hide/show a button in swift

。_饼干妹妹 提交于 2019-12-03 09:37:48

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:

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.

BCHOKZ

SWIFT 3

I created an IBOutlet: loadingBDLogo

To Show:

loadingBDLogo.isHidden = false

To Hide:

self.loadingBDLogo.isHidden = true

Vijay yadav

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!