A. stein A. stein - 7 months ago 10
Swift Question

How to show/hide my UILabel ? - UIButton does not work?

I have a HUGE problem.
What I wish for:
I wish for a label which is hidden and uneditable when the view loads or when pressing a button besides the button with the tag "15", and by pressing the button with the tag "15" is the only way for the label to show up and be editable.

my problem:
my problem is that when the view did load, it is not hidden at all and when I press the other buttons it doesn't hide either.
I have tried to set the "label.hidden = true"
inside the "weak var label : UILabel! {...}" (where I define the label) but when I then press the button with the tag "15" it doesn't show up.

btw don't worry about the other button tags, they are all included in the
"buttonAclicked" function, i have not included them because i thought it would take to much space

import Foundation
import UIKit






class TriangleViewController : UIViewController {


@IBOutlet weak var Open: UIBarButtonItem!




@IBOutlet weak var display: UILabel!




var Anumber = ""
var Bnumber = ""
var Cnumber = ""
var Ynumber = ""
var Xnumber = ""
var znumber = ""




@IBOutlet var btnNine: UIButton!





@IBOutlet var btn : UIButton!
@IBOutlet var btnOne : UIButton!




@IBOutlet var btnThreeclicked : UIButton!





var tags = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var tagStrings = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."]



var btnArray = [UIButton]()
var i = 0
var activeInput = 0




@IBOutlet var buttona : UIButton!

@IBOutlet var buttonclicked : UIButton!




// override func viewDidAppear(animated: Bool) {


//MARK: LABEL over tastatur

weak var label : UILabel! {
var label = UILabel(frame: CGRectMake(0 * view.bounds.width, 0.2 * view.bounds.height, 1 * view.bounds.width, 0.06 * view.bounds.height))
// label.center = CGPointMake(0.5 * view.bounds.width, 0.57 * view.bounds.height)
label.backgroundColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Right
label.font = label.font.fontWithSize(28)
label.text = "0"
label.tag = 20

self.view.addSubview(label)



return label
}
// }
weak var labelB : UILabel! {
var labelB = UILabel(frame: CGRectMake(0 * view.bounds.width, 0.58 * view.bounds.height, 1.0 * view.bounds.width, 0.06 * view.bounds.height))
labelB.center = CGPointMake(0.5 * view.bounds.width, 0.57 * view.bounds.height)
labelB.backgroundColor = UIColor.whiteColor()
labelB.textAlignment = NSTextAlignment.Right
labelB.font = labelB.font.fontWithSize(28)
labelB.text = "0"
labelB.tag = 21
self.view.addSubview(labelB)

return labelB
}

weak var labelC : UILabel! {
var labelC = UILabel(frame: CGRectMake(0 * view.bounds.width, 0.58 * view.bounds.height, 1 * view.bounds.width, 0.06 * view.bounds.height))
labelC.center = CGPointMake(0.5 * view.bounds.width, 0.57 * view.bounds.height)
labelC.backgroundColor = UIColor.whiteColor()
labelC.textAlignment = NSTextAlignment.Right
labelC.font = labelC.font.fontWithSize(28)
labelC.text = "0"
labelC.tag = 22
self.view.addSubview(labelC)

return labelC
}

weak var labelY : UILabel! {
var labelY = UILabel(frame: CGRectMake(0 * view.bounds.width, 0.58 * view.bounds.height, 1 * view.bounds.width, 0.06 * view.bounds.height))
labelY.center = CGPointMake(0.5 * view.bounds.width, 0.57 * view.bounds.height)
labelY.backgroundColor = UIColor.whiteColor()
labelY.textAlignment = NSTextAlignment.Right
labelY.font = labelY.font.fontWithSize(28)
labelY.text = "0"
labelY.tag = 23
self.view.addSubview(labelY)

return labelY
}

weak var labelX : UILabel! {
var labelX = UILabel(frame: CGRectMake(0 * view.bounds.width, 0.58 * view.bounds.height, 1 * view.bounds.width, 0.06 * view.bounds.height))
labelX.center = CGPointMake(0.5 * view.bounds.width, 0.57 * view.bounds.height)
labelX.backgroundColor = UIColor.whiteColor()
labelX.textAlignment = NSTextAlignment.Right
labelX.font = labelX.font.fontWithSize(28)
labelX.text = "0"
labelX.tag = 24
self.view.addSubview(labelX)

return labelX
}





override func viewDidLoad() {
Open.target = self.revealViewController()
Open.action = Selector("revealToggle:")










display.hidden = true




label.hidden = true
labelB.hidden = true
labelC.hidden = true
labelX.hidden = true
labelY.hidden = true

//MARK: TASTATUR KNAPPER
let btn = UIButton()
btn.frame = CGRectMake(0.04 * view.bounds.width, 0.91 * view.bounds.height, 0.44 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btn.setTitle("0", forState: .Normal) //set button title
btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btn.backgroundColor = UIColor.darkGrayColor() //set button background color
btn.tag = 0 // set button tag
btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btn) //add button in view
btnArray.append(btn)


let btnDot = UIButton()
btnDot.frame = CGRectMake(0.52 * view.bounds.width, 0.91 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnDot.setTitle(".", forState: .Normal) //set button title
btnDot.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnDot.backgroundColor = UIColor.darkGrayColor() //set button background color
btnDot.tag = 10 // set button tag
btnDot.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnDot) //add button in view


let btnOk = UIButton()
btnOk.frame = CGRectMake(0.76 * view.bounds.width, 0.73 * view.bounds.height, 0.2 * view.bounds.width, 0.25 * view.bounds.height) //set frame
btnOk.setTitle("OK", forState: .Normal) //set button title
btnOk.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnOk.backgroundColor = UIColor.orangeColor() //set button background color
btnOk.tag = 11 // set button tag
btnOk.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnOk) //add button in view

let btnOne = UIButton()
btnOne.frame = CGRectMake(0.04 * view.bounds.width, 0.82 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnOne.setTitle("1", forState: .Normal) //set button title
btnOne.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnOne.backgroundColor = UIColor.darkGrayColor() //set button background color
btnOne.tag = 1 // set button tag
btnOne.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnOne) //add button in view
btnArray.append(btnOne)


let btnTwo = UIButton()
btnTwo.frame = CGRectMake(0.28 * view.bounds.width, 0.82 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnTwo.setTitle("2", forState: .Normal) //set button title
btnTwo.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnTwo.backgroundColor = UIColor.darkGrayColor() //set button background color
btnTwo.tag = 2 // set button tag
btnTwo.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnTwo) //add button in view

let btnThree = UIButton()
btnThree.frame = CGRectMake(0.52 * view.bounds.width, 0.82 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnThree.setTitle("3", forState: .Normal) //set button title
btnThree.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnThree.backgroundColor = UIColor.darkGrayColor() //set button background color
btnThree.tag = 3 // set button tag
btnThree.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnThree) //add button in view

// btnThreeclicked.userInteractionEnabled = false





let btnFour = UIButton()
btnFour.frame = CGRectMake(0.04 * view.bounds.width, 0.73 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnFour.setTitle("4", forState: .Normal) //set button title
btnFour.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnFour.backgroundColor = UIColor.darkGrayColor() //set button background color
btnFour.tag = 4 // set button tag
btnFour.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnFour) //add button in view


let btnFive = UIButton()
btnFive.frame = CGRectMake(0.28 * view.bounds.width, 0.73 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnFive.setTitle("5", forState: .Normal) //set button title
btnFive.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnFive.backgroundColor = UIColor.darkGrayColor() //set button background color
btnFive.tag = 5 // set button tag
btnFive.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnFive) //add button in view

let btnSix = UIButton()
btnSix.frame = CGRectMake(0.52 * view.bounds.width, 0.73 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnSix.setTitle("6", forState: .Normal) //set button title
btnSix.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnSix.backgroundColor = UIColor.darkGrayColor() //set button background color
btnSix.tag = 6 // set button tag
btnSix.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnSix) //add button in view

let btnSeven = UIButton()
btnSeven.frame = CGRectMake(0.04 * view.bounds.width, 0.64 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnSeven.setTitle("7", forState: .Normal) //set button title
btnSeven.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnSeven.backgroundColor = UIColor.darkGrayColor() //set button background color
btnSeven.tag = 7 // set button tag
btnSeven.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnSeven) //add button in view

let btnEight = UIButton()
btnEight.frame = CGRectMake(0.28 * view.bounds.width, 0.64 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnEight.setTitle("8", forState: .Normal) //set button title
btnEight.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnEight.backgroundColor = UIColor.darkGrayColor() //set button background color
btnEight.tag = 8 // set button tag
btnEight.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnEight) //add button in view

let btnNine = UIButton()
btnNine.frame = CGRectMake(0.52 * view.bounds.width, 0.64 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnNine.setTitle("9", forState: .Normal) //set button title
btnNine.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnNine.backgroundColor = UIColor.darkGrayColor() //set button background color
btnNine.tag = 9 // set button tag
btnNine.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnNine) //add button in view


let btnClear = UIButton()
btnClear.frame = CGRectMake(0.76 * view.bounds.width, 0.64 * view.bounds.height, 0.2 * view.bounds.width, 0.07 * view.bounds.height) //set frame
btnClear.setTitle("C", forState: .Normal) //set button title
btnClear.setTitleColor(UIColor.whiteColor(), forState: .Normal) //set button title color
btnClear.backgroundColor = UIColor.darkGrayColor() //set button background color
btnClear.tag = 99 // set button tag
btnClear.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
self.view.addSubview(btnClear) //add button in view







//MARK: Knapper paa vinkler og siderne



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.tag = 15
buttona.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside)
self.view.addSubview(buttona)


let buttonb = UIButton()
buttonb.frame = CGRectMake(0.47 * view.bounds.width, 0.463 * view.bounds.height, 22, 22)
buttonb.layer.cornerRadius = 0.04 * view.bounds.width
buttonb.backgroundColor = UIColor.greenColor()
buttonb.setImage(UIImage(named:"B.png"), forState: .Normal)
buttonb.tag = 16
buttonb.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside)
view.addSubview(buttonb)


let buttonc = UIButton()
buttonc.frame = CGRectMake(0.47 * view.bounds.width, 0.32 * view.bounds.height, 22, 22)
buttonc.layer.cornerRadius = 0.04 * view.bounds.width
buttonc.backgroundColor = UIColor.greenColor()
buttonc.setImage(UIImage(named:"C.png"), forState: .Normal)
buttonc.tag = 17
buttonc.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside)
view.addSubview(buttonc)

let buttony = UIButton()
buttony.frame = CGRectMake(0.772 * view.bounds.width, 0.17 * view.bounds.height, 22, 22)
buttony.layer.cornerRadius = 0.04 * view.bounds.width
buttony.backgroundColor = UIColor.greenColor()
buttony.setImage(UIImage(named:"Y.png"), forState: .Normal)
buttony.tag = 18
buttony.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside)
view.addSubview(buttony)

let buttonx = UIButton()
buttonx.frame = CGRectMake(0.19 * view.bounds.width, 0.463 * view.bounds.height, 22, 22)
buttonx.layer.cornerRadius = 0.04 * view.bounds.width
buttonx.backgroundColor = UIColor.greenColor()
buttonx.setImage(UIImage(named:"X.png"), forState: .Normal)
buttonx.tag = 19
buttonx.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside)
view.addSubview(buttonx)

let buttonz = UIButton()
buttonz.frame = CGRectMake(0.772 * view.bounds.width, 0.463 * view.bounds.height, 22, 22)
buttonz.layer.cornerRadius = 0.04 * view.bounds.width
buttonz.backgroundColor = UIColor.greenColor()
buttonz.setImage(UIImage(named:"Z.png"), forState: .Normal)
buttonz.addTarget(self, action: "buttonAclicked:", forControlEvents: .TouchUpInside)
view.addSubview(buttonz)











print("\(btnThreeclicked)")



self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}




func btnclicked(sender: UIButton) {


// sender.currentTitle!






switch activeInput {
case 1 :
if sender.currentTitle == "OK" {
print("hey")
} else {
Anumber += sender.currentTitle!
label.text = Anumber
print("text: A \(Anumber) \(Bnumber) \(Cnumber) \(Ynumber) \(Xnumber)")
activeInput = 1
}
break

case 2 :
Bnumber += sender.currentTitle!
labelB.text = Bnumber
print("text: B \(Anumber) \(Bnumber) \(Cnumber) \(Ynumber) \(Xnumber)")
activeInput = 2
break
case 3 :
Cnumber += sender.currentTitle!
labelC.text = Cnumber
print("text: C \(Anumber) \(Bnumber) \(Cnumber) \(Ynumber) \(Xnumber)")
activeInput = 3
break
case 4 :
Ynumber += sender.currentTitle!
labelY.text = Ynumber
print("text: Y \(Anumber) \(Bnumber) \(Cnumber) \(Ynumber) \(Xnumber)")
activeInput = 4
break
case 5 :
Xnumber += sender.currentTitle!
labelX.text = Xnumber
print("text: X \(Anumber) \(Bnumber) \(Cnumber) \(Ynumber) \(Xnumber)")
activeInput = 5
break
default :
print("wrong")
activeInput = 0
}


}



func buttonAclicked(sender: UIButton) {


switch sender.tag {
case 11 :
print("det virker squ")
self.label.hidden = true
labelB.hidden = true
labelC.hidden = true
labelY.hidden = true
labelX.hidden = true
break


//MARK: KNAPPERNE P{ TREKANTERNE CASES
case 15 :
print("button A was pressed")
self.label.hidden = false
labelB.hidden = true
labelC.hidden = true
labelY.hidden = true
labelX.hidden = true

label.setNeedsDisplay()
activeInput = 1
break
case 16 :
print("button B was pressed")
self.label.hidden = true

labelC.hidden = true
labelY.hidden = true
labelX.hidden = true
labelB.hidden = false
labelB.setNeedsDisplay()
activeInput = 2
break
case 17 :
print("button C was pressed")
self.labelB.hidden = true
label.hidden = true
labelY.hidden = true
labelX.hidden = true
labelC.hidden = false
labelC.setNeedsDisplay()
activeInput = 3
break
case 18 :
print("button Y was pressed")
label.hidden = true
labelC.hidden = true
labelY.hidden = false
labelX.hidden = true
labelB.hidden = true
labelY.setNeedsDisplay()
activeInput = 4
break
case 19 :
print("button X was pressed")
label.hidden = true
labelC.hidden = true
labelY.hidden = true
labelX.hidden = false
labelB.hidden = true
labelX.setNeedsDisplay()
activeInput = 5
break

default :

print("wrong button")
activeInput = 0

}





}











}

Answer

Have you tried : self.label.hidden = true ?

Furthermore, why not use an IBOutlet ? It makes EVERYTHING much easier.

Please tell us if this fixed your issue.

EDIT: Hmmm. This is how I would correct your code (I only included the places where I modified it):

import Foundation
import UIKit

class TriangleViewController : UIViewController {

var label : UILabel!

// override func viewDidAppear(animated: Bool) {


//MARK: LABEL over tastatur

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

        self.label = UILabel(frame: CGRectMake(0 * view.bounds.width, 0.2 * view.bounds.height, 1 * view.bounds.width, 0.06 * view.bounds.height))
        self.label.center = CGPointMake(0.5 * view.bounds.width, 0.57 * view.bounds.height)
        self.label.backgroundColor = UIColor.whiteColor()
        self.label.textAlignment = NSTextAlignment.Right
        self.label.font = label.font.fontWithSize(28)
        self.label.text = "0"
        self.label.tag = 20

        self.view.addSubview(label)
        self.label.hidden = true
}

func buttonAclicked(sender: UIButton) {


    switch sender.tag {
    case 11 :
        print("det virker squ")
        self.label.hidden = true

        break


    //MARK: KNAPPERNE P{ TREKANTERNE CASES
    case 15 :
        print("button A was pressed")
        self.label.hidden = false

        label.setNeedsDisplay()

        break
    case 16 :
        print("button B was pressed")
        self.label.hidden = true

        break
    case 17 :
        print("button C was pressed")
        self.label.hidden = true

        break
    case 18 :
        print("button Y was pressed")
        label.hidden = true

        break
    case 19 :
        print("button X was pressed")
        label.hidden = true

        break

    default :
        print("wrong button")
    }
}
}

SOLUTION :

1) Put self.label where I put it.

2) Make good use of ViewDidAppear().

3) Don't make your label a weak var ! In this case, nil was found because the label wasn't kept long enough in memory.