Swift 如何显示/隐藏我的UILabel?-UIButton不工作?
我有个大问题。 我的愿望是: 我希望标签在加载视图时隐藏且不可编辑,或者在按下标签为“15”的按钮之外的按钮时,按下标签为“15”的按钮是标签显示和可编辑的唯一方式 我的问题是: 我的问题是,当视图加载时,它根本不隐藏,当我按下其他按钮时,它也不隐藏。 我已尝试设置“label.hidden=true” 在“弱变量标签:UILabel!{…}”(我定义标签的地方)中,但是当我按下带有标签“15”的按钮时,它没有显示 顺便说一句,不要担心其他按钮标签,它们都包含在 “ButtonClicked”功能,我没有包括它们,因为我认为这会占用很多空间Swift 如何显示/隐藏我的UILabel?-UIButton不工作?,swift,button,label,edit,show-hide,Swift,Button,Label,Edit,Show Hide,我有个大问题。 我的愿望是: 我希望标签在加载视图时隐藏且不可编辑,或者在按下标签为“15”的按钮之外的按钮时,按下标签为“15”的按钮是标签显示和可编辑的唯一方式 我的问题是: 我的问题是,当视图加载时,它根本不隐藏,当我按下其他按钮时,它也不隐藏。 我已尝试设置“label.hidden=true” 在“弱变量标签:UILabel!{…}”(我定义标签的地方)中,但是当我按下带有标签“15”的按钮时,它没有显示 顺便说一句,不要担心其他按钮标签,它们都包含在 “ButtonClicked”功
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
}
}
}
您是否尝试过:
self.label.hidden=true
此外,为什么不使用IBOutlet呢?它使一切变得容易多了
请告诉我们这是否解决了您的问题
编辑:Hmmm。这就是我修改代码的方式(我只包括修改代码的地方):
解决方案:
1) 将self.label
放在我放的地方
2) 充分利用viewdispect()
3) 不要让你的
标签a弱var!在本例中,由于标签在内存中保存的时间不够长,因此找到了nil。问题是您使用view.bounds.width和view.bounds.height作为标签大小,但视图本身尚未初始化以显示,因此此时两者都为零。
将标签代码移动到ViewWillDisplay,它将正常工作
class TriangleViewController : UIViewController {
var label: UILabel? = nil;
override func viewWillAppear(animated: Bool) {
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!)
}
}
由于我已将其作为一个选项变量:UILabel?,因此需要将其展开才能使用。比如:
标签!或者
如果let label=label{…}
此答案应标记为正确。我刚刚测试了代码,将self.label.hidden=true放入viewDidLoad将起作用。Ghulam Ali的说法是正确的,因为视图尚未初始化,所以标签的位置是错误的。在viewDidLoad中设置帧/位置也会起作用。我现在已经尝试过了,第一部分可以起作用,但按下按钮时除外(不是标记“15”)这应该会再次隐藏标签,但什么也没有发生:/i我尝试在switch语句中写入self.label.hidden=false,但仍然没有work@A.stein请更新您的代码,以便我可以看到您尝试放置的位置self.label.hidden
到目前为止;)@A.stein请将每个标签
替换为self.label
,并告诉我们进展情况:D@Coder1000什么也没发生:/但我必须说我很感谢你的帮助:)请不要忘记将答案标记为正确答案(如果它帮助了你/解决了你的问题)。我知道你没有接受任何问题的答案。由于您是新来的,下面是您的操作方法:单击答案旁边的复选标记。欢迎来到SO。
class TriangleViewController : UIViewController {
var label: UILabel? = nil;
override func viewWillAppear(animated: Bool) {
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!)
}
}