定时器完成后执行segue

时间:2017-01-24 13:30:27

标签: ios swift

我试图在计时器达到0后立即执行segue。我的代码如下所示,用于viewDidLoad中的计时器:

override func viewDidLoad() {
        super.viewDidLoad()

        if timerRunning == false {
            timerCounter = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(Counting), userInfo: nil, repeats: true)
            timerRunning = true
        }

        if counter == 0 {
            timerRunning = false
        }

这是功能计数器:

func Counting() {

    counter -= 1
    timer.text = "\(counter)"
    if counter == 0 {
        timerCounter.invalidate()
        timerRunning = false
        self.performSegue(withIdentifier: "resultView", sender: self)
    }
}

2 个答案:

答案 0 :(得分:0)

您只需要在主线程上执行segue。使用

DispatchQueue.main.async{
   self.performSegue(withIdentifier: "resultView", sender: self)
}

答案 1 :(得分:0)

如果您确定segue标识符对performSegue调用以及在storyboard或xib中都是正确的,那么也许self会以某种方式在它应该被解除分配之前被解除分配?

试试这个:

DispatchQueue.main.async{

    [weak self] in

    self?.performSegue(WithIdentifier:"resultView", sender: self)

}

或者在运行performSegue之前尝试自选链接自我以确定。