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
label.isHidden = true // hide
label.isHidden = false // show
you can use isHidden
with other ui objects, see that answer also
Create an IBAction:
@IBAction func thumbsUpButtonaPressed(sender: UIButton) {
label.hidden = false
}
Then connect it with your button by cmd + drag on the button to the action:
Swift 5 Update
@IBAction func thumbsUpButtonaPressed(sender: UIButton) {
label.isHidden = false
}
Unless I am missing something in viewDidLoad you are creating a new label
var label = ...
you are not using the IBOutlet Property like
label = ...
Also are you sure your brackets are correct because it looks like your buttonPressed method is nested inside viewDidLoad.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With