how to hide/show a button in swift

前端 未结 4 1873
攒了一身酷
攒了一身酷 2021-02-12 03:16

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

4条回答
  •  我在风中等你
    2021-02-12 04:15

    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.

提交回复
热议问题