如何在swift中隐藏/显示按钮 [英] how to hide/show a button in swift
问题描述
我正在尝试使用if语句在标签显示特定状态时隐藏按钮,并在标签显示其他内容时显示。标签的名称是Status,当它显示Closed
时,我想隐藏它,当它显示Open
,它会出现。
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
}
}
}
推荐答案
正如@LAmasse所说,你想使用 button.hidden = true
。 button.hidden
在Swift 3中重命名为 button.isHidden
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
}
什么是购买?从大写的名字来看,它似乎是一个阶级。如果是这样,表达式 Purchase()
可能会创建 Purchase
类的新实例,这没有任何意义。你为什么打电话?如果这是创建一个新的购买
对象,那么该代码是没有意义的。 (您将在 if
语句中创建一个新对象,该语句将在下一行中被丢弃,因为您没有对其进行强引用。)
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.)
您想为按钮设置IBOutlet并在Interface Builder中连接它。
You want to set up an IBOutlet for your button and connect it in Interface Builder.
声明可能如下所示:
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
}
}
}
}
我不清楚为什么你要循环来自你的所有结果查询并显示按钮,如果任何结果的StatusType是==已关闭。
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".
最后,我对解析不是很熟悉。如果没有在主线程上调用findObjectsInBackgroundWithBlock方法的完成块,则必须更改该代码以在主线程上进行UI更新。
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.
我已经知道Parse在主线程上执行完成处理程序,所以你不必担心来自Parse完成处理程序的UI调用。
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.
这篇关于如何在swift中隐藏/显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!