Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide/show a button in swift

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         }     } } 
like image 983
Tom F Avatar asked May 05 '15 23:05

Tom F


People also ask

How do I hide the action button in Swift?

Just create an IBOutlet of the same button and set its isHidden property to true once it's tapped. Save this answer.

How do you hide a view in Objective C?

hidden = YES; // or view. hidden = NO; or by calling setHidden: [view setHidden:YES]; // or [view setHidden:NO];


2 Answers

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.

like image 186
Duncan C Avatar answered Sep 17 '22 15:09

Duncan C


SWIFT 3

I created an

IBOutlet: loadingBDLogo 

To Show:

loadingBDLogo.isHidden = false 

To Hide:

self.loadingBDLogo.isHidden = true 
like image 34
BCHOKZ Avatar answered Sep 17 '22 15:09

BCHOKZ