Search code examples
iosswifttimerviewdidappearhiddenfield

Swift iOS- How to hide label then make it appear after a certain time period


I have a label that gets hidden when a button is pressed. After a certain time period like 60 secs I want the label to reappear. I'd assume I do that in viewDidAppear, How would i do that?

@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
}

Solution

  • @IBAction func buttonTapped(_ sender: UIButton){
        self.myLabel.isHidden = true
        DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
            self.myLabel.isHidden = false
        }
    }