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

11,887

Solution 1

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

Solution 2

You can do this by scheduling a timer:

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
    }
}
Share:
11,887
Lance Samaria
Author by

Lance Samaria

Self taught iOS developer from the Marble Hill section of the Bronx NY. ARKit novice but interested in advanced topics.

Updated on June 04, 2022

Comments

  • Lance Samaria
    Lance Samaria almost 2 years

    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
    }