0

Folks, I have searched and read and I cannot figure out why my timer is not firing in my segue until I have closed the view for the segue and returned to my ViewController view.

So, at the risk of duplication, I am going to post my code and hope someone has some kind of suggestion as to what I can do to figure this out.

I have a game and all my code has primarily been in the ViewController class and I am ready to start breaking it apart into other classes. So, I am working on a new PlayGameViewController that will replace the methods that relate to game play in my main ViewController class. The PlayGameViewController is being instantiated and presented via a modal segue after a button is clicked in the ViewController view.

Once the PlayGameViewController is displayed I set up a timer (named timer1 to not be confused with the timer in the ViewController class) by calling a startTimer() method. I have the updateViewsWithTimer as an objc func and it never gets fired until the view for the PlayGameViewController view is closed, via clicking the red dot in the upper left of the PlayViewController view.

I put in print statements to help me debug and I can see that timer1 is, in fact, being instantiated and it is on the main thread, but the updateViewsWithTimer objc func never gets called until I close the PlayGameViewController view.

I found a couple of articles such as this article and this one that appear to try to remedy this very issue, but it is not in a segue and their problem doesn't suddenly start to work by closing a view.

Here are my code snippets:

import Cocoa
class PlayGameViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSTextFieldDelegate {
    // EDIT: Changed this back to the original I use in my code
    var timer1 = Timer()
    override viewDidLoad() {
        super.viewDidLoad()
        startTimer1()
    }
    func startTimer1() {
        if Thread.isMainThread {
            if timer1.isValid {
                timer1.invalidate()
            }
            print("Preparing to schedule timer1.")
            timer1 = .scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateViewsWithTimer1), userInfo: nil, repeats: true)
            print("timer1 scheduled!")
        }
    }
    // EDIT: Added property timer: to method call as per Leo Dabus in the comments
    @objc func updateViewsWithTimer1(_ timer: Timer) {
        print("updateViewsWithTimer1 fired by timer1.")
    }
}

Here's the prepare method from my ViewController class if that helps.

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    guard let pvc = segue.destinationController as? PlayGameViewController else { return }
    pvc.view.frame.origin = .zero
}

Oh, and self is actually pointing to PlayViewController. I thought maybe that was an issue since I had the same method in ViewController, but that doesn't seem to be it either.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
SouthernYankee65
  • 1,129
  • 10
  • 22

0 Answers0