title: Start Timer in other viewcontroller in swift
body: <p>I have a problem with my timer functions. I start a timer when clicking on a btn <em>(startBtn)</em> and I show a new viewcontroller as a popover when hitting another btn <em>(restBtn)</em> Right now it start the timer correctly and opens the new viewcontroller as popover with the time counting down. </p>
  
  <p>The problem is when in the new viewcontroller it hits 0 and dismisses hisself. It should start the timer in the first viewcontrolle automatically.</p>
  
  <p><strong>Here is my code on mainVC:</strong></p>
  
  <pre><code>var timeLeft = 0
  var myTimer: Timer!
  
  
  @IBAction func startBtnPressed(_ sender: AnyObject) {
          timeLeft = 0
          myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(workoutStartVC.timerRunning), userInfo: nil, repeats: true)  
      }
  
  
  @IBAction func restBtnPressed(_ sender: AnyObject) {
          print("rest mode button is pressed and i am showing a overlay right now with data count down")
          myTimer.invalidate()
  
          // show popup when user interacts with restbtn
          let popOpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpVC
          self.addChildViewController(popOpVC)
          popOpVC.view.frame = self.view.frame
          popOpVC.view.backgroundColor = BLUE_COLOR.withAlphaComponent(0.75)
          self.view.addSubview(popOpVC.view)
          popOpVC.didMove(toParentViewController: self)
  
          timeLbl.text = "" 
      }
  
  func timerRunning() {
          timeLeft = timeLeft + 1
          print("the timeLeft is: \(timeLeft)")
  
          **timeLbl.text = "\(timeLeft)"** (HERE I AM GETTING ON THE SECOND TIME WHEN DISMISSING THE POPUPVC A FOUND NILL)
  
          if timeLeft == 30 {
              myTimer.invalidate()
              timeLbl.text = ""
          }
      }
  </code></pre>
  
  <p><strong>Here is my code on the PopUpVC:</strong></p>
  
  <pre><code>var timeLeft = 0
      var myTimer: Timer!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          timeLeft = 10
          myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(PopUpVC.timerRunning), userInfo: nil, repeats: true)
      }
  
  
      func timerRunning() {
          timeLeft = timeLeft - 1
          restModeTimer.text = "\(timeLeft)"
  
          if timeLeft == 0 {
              myTimer.invalidate()
              restModeTimer.text = ""
              self.view.removeFromSuperview()
  
              let worskoutStartVC = workoutStartVC()
              worskoutStartVC.myTimer = nil
              worskoutStartVC.timeLeft = 0
  
              worskoutStartVC.myTimer = Timer.scheduledTimer(timeInterval: 1.0, target: worskoutStartVC, selector: #selector(worskoutStartVC.timerRunning), userInfo: nil, repeats: true)
          }
      }  
  </code></pre>
  
  <p>Can anybody explains why I get a found nil while unwrapping an option value at the part that I made bold in my code below?</p>
  
  <p>Thank you very much!</p>