按下按钮时,我有一个隐藏的标签。经过一段时间,如60秒,我希望标签重新出现。我假设我在viewDidAppear中这样做,我该怎么做?
@IBOutlet weak var myLabel: UILabel!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//after 60 secs myLabel should reappear
//self.myLabel.isHidden = false
}
@IBAction func buttonTapped(_ sender: UIButton){
self.myLabel.isHidden = true
}
答案 0 :(得分:11)
@IBAction func buttonTapped(_ sender: UIButton){
self.myLabel.isHidden = true
DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
self.myLabel.isHidden = false
}
}
答案 1 :(得分:1)
您可以通过安排计时器来执行此操作:
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
@IBAction func buttonTapped(sender: UIButton) {
if !myLabel.isHidden {
myLabel.isHidden = true
Timer.scheduledTimer(timeInterval: 15.0, target: self, selector: #selector(showLabel), userInfo: nil, repeats: false)
}
}
func showLabel() {
myLabel.isHidden = false
}
}