我试图隐藏标签,然后在按下按钮后,即使您退出/重新启动应用程序,标签也会永久显示。现在它显示我点击按钮后但在我退出/重启后变为隐藏状态。这是我的代码:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
hidePercent()
}
var didTapOptionsButtonForCell: ((YourPollsCell) -> Void)?
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
let percentColor = UIColor(red: 115/255, green: 21/255, blue: 54/255, alpha: 1.0).cgColor
pollText1Percent.layer.borderColor = percentColor
pollText1Percent.layer.borderWidth = 2
pollText2Percent.layer.borderColor = percentColor
pollText2Percent.layer.borderWidth = 2
}
func hidePercent() {
pollText1Percent.isHidden = true
pollText2Percent.isHidden = true
}
@IBAction func reportButtonTapped(_ sender: UIButton) {
didTapOptionsButtonForCell?(self)
}
@IBAction func pollText1ButtonTapped(_ sender: UIButton) {
delegate?.didTapVoteButton(sender, pollNum: 1, on: self)
pollText1Percent.isHidden = false
pollText2Percent.isHidden = false
}
@IBAction func pollText2ButtonTapped(_ sender: UIButton) {
delegate?.didTapVoteButton(sender, pollNum: 2, on: self)
pollText1Percent.isHidden = false
pollText2Percent.isHidden = false
}
答案 0 :(得分:0)
点击按钮时,您可以为UserDefaults添加值。
UserDefaults.standard.set(value: true, forKey: "specialButtonWasTapped")
然后在启动应用程序并设置isHidden属性时在控制器中检查此值。
if let wasTapped = UserDefaults.standard.value(forKey: "specialButtonWasTapped") {
specialLabel.isHidden = !wasTapped // false
}
else {
specialLabel.isHidden = true
}
或者清理这个想法
UserDefaults.standard.set(value: false, forKey: "labelShouldBeHidden")
然后您可以使用nil合并运算符
设置isHidden属性specialLabel.isHidden = (UserDefaults.standard.value(forKey: "labelShouldBeHidden") as? Bool) ?? true