ios 如何快速隐藏/显示按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30065010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:54:38  来源:igfitidea点击:

how to hide/show a button in swift

iosswiftbuttonhidden

提问by Tom F

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.

我正在尝试使用 if 语句,当标签显示特定状态时隐藏按钮,并在标签显示其他内容时出现。标签的名字是Status,显示的时候"Closed"我要隐藏,显示的"Open"时候就出现。

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
        }
    }
}

回答by Duncan C

As @LAmasse says, you want to use button.hidden = true. button.hiddenwas renamed to button.isHiddenin Swift 3

正如@LAmasse 所说,您想使用button.hidden = true. 在 Swift 3 中button.hidden更名为button.isHidden

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 Purchaseclass, which makes no sense. Why are you making a function call? If that is creating a new Purchaseobject then that code is pointless. (You would create a new object inside the ifstatement that would be discarded on the very next line since you don't keep a strong reference to it.)

什么是采购?从大写的名称来看,它似乎是一个类。如果是这样,则表达式Purchase()很可能会创建Purchase该类的新实例,这毫无意义。你为什么要进行函数调用?如果那是创建一个新Purchase对象,那么该代码毫无意义。(您将在if语句内创建一个新对象,该对象将在下一行被丢弃,因为您没有保留对它的强引用。)

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

你想为你的按钮设置一个 IBOutlet 并在 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".

我不清楚为什么要遍历查询的所有结果,并在任何结果的“StatusType”为 ==“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.

最后,我对解析不是很熟悉。如果在主线程上没有调用 findObjectsInBackgroundWithBlock 方法的完成块,您将必须更改该代码以在主线程上更新 UI。

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.

从那以后,我了解到 Parse 在主线程上执行其完成处理程序,因此您无需担心来自 Parse 完成处理程序的 UI 调用。

回答by BCHOKZ

SWIFT 3

斯威夫特 3

I created an IBOutlet: loadingBDLogo

我创建了一个 IBOutlet:loadingBDLogo

To Show:

显示:

loadingBDLogo.isHidden = false

loadingBDLogo.isHidden = false

To Hide:

隐藏:

self.loadingBDLogo.isHidden = true

self.loadingBDLogo.isHidden = true

回答by Vijay yadav

The sample code for hiding a button in Swift:

在 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.

如果你想保留对它的引用,你必须使 UIButton 成为类的一个属性。然后您可以使用 self.takePhotoButton 访问它。