0

I am trying to perform segue right after the timer reach 0. My code as below for the timer in the 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
        }

and this is the function counter:

func Counting() {

    counter -= 1
    timer.text = "\(counter)"
    if counter == 0 {
        timerCounter.invalidate()
        timerRunning = false
        self.performSegue(withIdentifier: "resultView", sender: self)
    }
}
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Yousef humdan
  • 67
  • 1
  • 1
  • 5

2 Answers2

0

You simply need to perform the segue on the main thread. Use

DispatchQueue.main.async{
   self.performSegue(withIdentifier: "resultView", sender: self)
}
mginn
  • 16,036
  • 4
  • 26
  • 54
0

If you are certain the segue identifier is correct for both the performSegue call and in storyboards or xib, then perhaps self is somehow being deallocated before it should?

try this:

DispatchQueue.main.async{

    [weak self] in

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

}

Or try optional chaining self before running the performSegue just to be sure.