我试图在按下按钮时创建/显示标签,然后在再次按下同一按钮时删除/隐藏相同标签。我试图在Swift中以编程方式执行此操作。在按下按钮后以编程方式删除UILabel
我试过使用label.removeFromSuperview(),但它似乎没有任何效果。然而,当我尝试在同一代码的位置移除按钮使用button.removeFromSuperview()
var label = UILabel()
let labelImage = UIImage(named: "Strike Line.png")
/* to select checkmarked state */
func pressCheck() {
let image = UIImage(named: "Checkmark.png")
button.setBackgroundImage(image, for: UIControlState.normal)
button.addTarget(self, action:#selector(self.pressUnCheck), for: .touchUpInside)
self.view.addSubview(button)
textField1.textColor = UIColor.gray //change textfield to a gray color
label = UILabel(frame: CGRect(x : 31, y : 69, width: 200, height: 2))
label.backgroundColor = UIColor(patternImage: labelImage!)
self.view.addSubview(label)
}
func pressUnCheck()
{
let image = UIImage(named: "To Be Completed Circle.png")
button.setBackgroundImage(image, for: UIControlState.normal)
button.addTarget(self, action:#selector(self.pressCheck), for: .touchUpInside)
self.view.addSubview(button)
label.removeFromSuperview()
textField1.textColor = UIColor.black
}
这里就是我试图删除/隐藏的标签。
你能展示更多的代码,比如你创建标签的位置以及你添加的位置吗? –
你确定这段代码正在执行吗? – JohnnyAW
增加了更多代码。 @JohnnyAW是的,我相信它正在执行,因为当我尝试label.backgroundColor = UIColor.red它会改变我的标签图像有一个红色时点击按钮 – brownmamba