[英]swift hiding label when a button is pressed
I have created my own label and my own button.我创建了自己的标签和按钮。 Now when the page loads the label hides as I want but when i click the button it does not show up as it supposed to do, in fact it does not do anything.现在,当页面加载标签时,标签会按照我的意愿隐藏,但是当我单击按钮时,它没有按预期显示,实际上它什么也没做。 How can I fix this problem which is making label show when i press the button?我该如何解决这个问题,当我按下按钮时会显示标签?
@IBOutlet var thumbsUpButtonaPressed : UIButton!
@IBOutlet weak var label : UILabel!
override func viewDidLoad() {
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "00000"
self.view.addSubview(label)
label.hidden = true
let buttona = UIButton()
buttona.frame = CGRectMake(0.772 * view.bounds.width, 0.32 * view.bounds.height, 22, 22)
buttona.layer.cornerRadius = 0.04 * view.bounds.width
buttona.backgroundColor = UIColor.greenColor()
buttona.setImage(UIImage(named:"A.png"), forState: .Normal)
buttona.addTarget(self, action: "thumbsUpButtonaPressed", forControlEvents: .TouchUpInside)
view.addSubview(button)
func thumbsUpButtonaPressed(sender: UIButton!) {
label.hidden = false
}
}
I am using below code on swift 3
我在swift 3
上使用以下代码
label.isHidden = true // hide
label.isHidden = false // show
you can use isHidden
with other ui objects, see that answer also您可以将isHidden
与其他 ui 对象一起使用,也请参阅该答案
Unless I am missing something in viewDidLoad you are creating a new label除非我在 viewDidLoad 中遗漏了一些东西,否则您正在创建一个新标签
var label = ...
you are not using the IBOutlet Property like你没有像使用 IBOutlet 属性
label = ...
Also are you sure your brackets are correct because it looks like your buttonPressed method is nested inside viewDidLoad.您还确定您的括号是正确的,因为看起来您的 buttonPressed 方法嵌套在 viewDidLoad 中。
Create an IBAction:创建一个 IBAction:
@IBAction func thumbsUpButtonaPressed(sender: UIButton) {
label.hidden = false
}
Then connect it with your button by cmd + drag on the button to the action:然后通过 cmd 将它与您的按钮连接 + 将按钮拖到操作上:
Swift 5 Update斯威夫特 5 更新
@IBAction func thumbsUpButtonaPressed(sender: UIButton) {
label.isHidden = false
}
You can also change:您还可以更改:
label.alpha = 1.0 // show
label.alpha = 0.0 // hide
Try to correct your function with:尝试使用以下方法更正您的功能:
func thumbsUpButtonaPressed(sender: UIButton!) {
print("button was pressed")
label.hidden = false
label.setNeedDisplay()
}
Create normal IBAction for your button:为您的按钮创建普通的 IBAction:
@IBAction func thumbsUpButtonaPressed(sender: UIButton!) {
label.hidden = false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.