1

I have a really basic two-view timer app I'm working on, wherein on View1 I have 30 buttons (15 buttons start timers, 15 of them invalidate each respective timer) and on View2 I have some other functionality not pertinent to my issue.

The issue is that my user switches back and forth between these two views while the timers are still running - the timers will still increment as normal when switching between the views but will cease updating their respective labels once they are switched back and forth.

The timers are implemented as such:

switch timedBehavior {
        case "Introduction":
            timer1 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: true)

        case "Observing Stationary":
            timer2 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action2), userInfo: nil, repeats: true)

        case "Observing Moving":
            timer3 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action3), userInfo: nil, repeats: true)

default:
            print("Error in Timer Button Func")
}

The timer invalidating buttons are implemented as:

switch stopButtons {

        case "stpBtn1":
            timer1.invalidate()
        case "stpBtn2":
            timer2.invalidate()
        case "stpBtn3":
            timer3.invalidate()
default:
        print("Error in Stop Button Func")

}

And each timer performs this functionality: (increments a number and updates a label)

func action()
{
    totalTime1 += 1
    timeLabel1.text = String(totalTime1)
}

So far I have tried to invalidate and immediately restart a particular timer if it was running in viewDidLoad() - which actually seemingly created two timers and doubled the speed of my increments.

I'm not very well versed with Swift unfortunately and am at a bit of a loss -- any help or even ideas on better implementations would be really appreciated. Thanks!

doomducky
  • 53
  • 7
  • are you invalidating the timer on viewwilldisappear or thats not wanted – zombie Mar 02 '17 at 03:21
  • 1
    Are you using segues to change between the 2 views? If so, you should use an unwind segue to return to VC1. – vacawama Mar 02 '17 at 03:25
  • @vacawama I am using segues, yes -- haven't heard of unwind segue will absolutely look into it now. – doomducky Mar 02 '17 at 03:29
  • 1
    A normal segue creates a new destination VC every time. So you are creating more and more VC's by segueing between them. That is why your labels aren't updating, because you are looking at a new VC1. – vacawama Mar 02 '17 at 03:30
  • @zombie yea the timers are not invalidated when the views switch -- the hopeful use case is that they will continue to function as normal when switching between the views and only invalidated when the user selects the button to invalidate them – doomducky Mar 02 '17 at 03:31
  • 1
    See: http://stackoverflow.com/q/13106095/1630618 – vacawama Mar 02 '17 at 03:32
  • @vacawama yea this looks exactly like what I needed -- thank you!! Trying to implement it now; the one issue I have is that I switch between views via. swipe gesture -- would I control drag the appropriate swipe gesture recognizer to the exit icon? – doomducky Mar 02 '17 at 03:43
  • I think you'll need to call the unwind segue with `performSegueWithIdentifier` in your swipe handler function. Look at the second half of this answer for how to set up the segue and give it an identifier. http://stackoverflow.com/a/30992088/1630618 – vacawama Mar 02 '17 at 03:47

1 Answers1

1

You are using segues to transition between VC1 and VC2. When you return from VC2, you are creating an entirely new VC1 which is why you don't see your labels updating.

You should use an unwind segue to return to VC1 from VC2. See here for how to setup and use an unwind segue.

Since you are using swipe gestures to transition between views, you will need to call the unwind segue programmatically. See the second half of this answer for how to set up the unwind segue and give it an identifier so that you can call it with performSegue(withIdentifier:sender:) from your swipe handler function.

Community
  • 1
  • 1
vacawama
  • 150,663
  • 30
  • 266
  • 294