Would this be a case for needing to "unwind" a segue?

I’ve got a sports timing app project.

Over 3 periods you record time spent in offense versus defensive zones.

The “show” segues (navigation controller) guide you from period 1 to 3, then a results page (arrays populating an outlet collection).

HOWEVER: When I navigate backwards (maybe there was a time out or other when the clock was stopped but I wanted to go look around) the clock values are there, but any changes to the clocks don’t get passed to the results VC.

Also, if I go backwards over a period (let’s say from 3 … skip 2… go right to 1st), and THEN go back to period 2—the period 2 clock is erased.

Is this a case where I need to be unwinding every time I move backwards?

I thought I was being clever by having the last line of each timer() writing the clock’s output to the array on the results page so that every time you’re physically using the clock you are updating the results page.

I guess not.

I have a suggestion. Mind you, I have no idea if it is sensible.

Create a struct which has two properties. One to store the time at the point the clock was stopped and the other to store the period. Create an instance of that struct in which you save the relevant times for the relevant periods

Each time you segue to the period to interrogate the time, pass that instance of the struct as a parameter. So period 1 is index 0, period 2 is index 1… yada yada.

Does that make sense or is that something that might work?

At what point do you zero those times anyway?

I see what you mean.

To answer your question: You can zero the times whenever it suits you, and you could technically pause the clocks and tap forward to the results–though the times would not reflect a finished game at that time. That would be more of a garbage-in garbage-out moment. I just didn’t want someone to be locked into only using the period timer once, and then you MUST move on. What if I made a mistake and just wanted to start my own times over? I just felt it friendly to be able to go back and forth, but the results would always show the times as they were when last paused.

A few Q’s:

This struct; are we talking putting it as an extension it’s own swift file, and each periods timers reference that? Right now (it’s sloppy I know) each VC has it’s own timers sending to the results page (updating the variables that get pasted in the ‘results sheet’.

Also, does it matter that I have a button on each VC doing a "perform segue with < next VC> " rather than having it on the Navigation Bar?

As I’m writing this I feel like I should clean this all up with a single Timer() in a func, in its own swift file to reduce code (right tnow each VC has it’s own timer).

Also, make a struct for each period, there in that same swift file with the timer-- as you described.

Without knowing the intricacies of your App architecture it’s a bit difficult to make a suggestion to fit.

Are the timers just keeping track of the elapsed time (or count down time) in the game? For example, in a basketball game you stop and start the timer depending on the state of play. Do you need separate timers? Can the ViewController that you are using to display the period be configured such that it can serve whichever period you are in? Can you configure a struct that would be capable of handling all of the data related to each period and from your Main View Controller tap on a button to select the period and trigger the same segue and pass the data related to that period?

Your Results VC could feed off that struct to summarise the game state.

Do you intend to record a lot of stats related to each period?.. timeouts, turnovers, goals scored, fouls, penalties etc?

I’m just using basketball as an example of course.

Right now, I can go back from 3 to 2, or 2 to 1 and see what was there on the clock (which is just a label.text). For any of those, if I go forward (1 to 2, or 2 to 3, or main to 1) the clock gets wiped.

How would this work with a struct?

  1. inserting a line of code at the end of the timer(s) that copies the clock value to the proper variable

  2. in the ViewWillAppear(), telling the clock’s label.text to produce the value from #1—be it an initialized “00:00.0” or the value from the timer.

This is the timer code I made. Irresponsibly it sits in each period’s VC. I will eventually clean it up to a single .swift to share instances.

@objc func greenKeepTimer() {
greenFractions += 1
if greenFractions > 99 {
greenSeconds += 1
greenFractions = 0
}

    if greenSeconds == 60 {
        greenMinutes += 1
        greenSeconds = 0
    }
    
    let greenMinutesString = greenMinutes > 9 ? "\(greenMinutes)" : "0\(greenMinutes)"
    let greenSecondsString = greenSeconds > 9 ? "\(greenSeconds)" : "0\(greenSeconds)"
    let greenFractionsString = greenFractions > 9 ? "\(greenFractions)" : "0\(greenFractions)"
    greenClock.text = "\(greenMinutesString):\(greenSecondsString).\(greenFractionsString)"
   
}

@Chris_Parker

The problem appears to be instantiation. Each time I go forward to the VC it’s a new instance of it.

Apple Documentation is not very helpful at how to keep using the same VC instantiation.