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
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.
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 func thumbsUpButtonaPressed(sender: UIButton!) {
label.hidden = false
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.