【问题标题】:Swift iOS- How to hide label then make it appear after a certain time period [duplicate]Swift iOS-如何隐藏标签然后使其在一段时间后出现[重复]
【发布时间】:2017-08-31 01:35:14
【问题描述】:

我有一个在按下按钮时隐藏的标签。在 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
}

【问题讨论】:

  • 你试过用谷歌搜索吗?
  • 在你问之前请谷歌:-) stackoverflow.com/questions/27990085/…
  • @ Steffen Lund Andersen 它没有引起我的注意,但你的权利,我应该只是用谷歌搜索它。谢谢,因为这是我正在寻找的答案:)!

标签: ios swift timer viewdidappear hiddenfield


【解决方案1】:
@IBAction func buttonTapped(_ sender: UIButton){
    self.myLabel.isHidden = true
    DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
        self.myLabel.isHidden = false
    }
}

【讨论】:

    【解决方案2】:

    您可以通过安排一个计时器来做到这一点:

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

    【讨论】:

    • 我对你的答案投了赞成票,但我已经接受了 Max 的答案。谢谢! :)
    • 请注意,每次点击按钮时,您都会添加另一个计时器时间表。
    • @AhmadF 谢谢!我通过检查myLabel.isHidden 更新了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多