0

Let say I have two view controllers, View controller A and View controller B

class ViewControllerA: UIViewController {
     override func viewDidLoad() {
        super.viewDidLoad()
        // it runs this function every 5 seconds
         Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)
    }

    @IBAction func callViewControllerBButtonClicked(_ sender: UIButton) {
         if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerBID") as? ViewControllerB {

                self.present(vc, animated: true, completion: nil)
        }

    }

    func printNumber() {
      print(0)
    }
}

Whenever someone clicks callViewControllerBButtonClicked() button, it will instantiate a new view controller which is ViewControllerB and sort of present it on top of ViewControllerA. The problem that I'm facing right now is even though I'm already on ViewControllerB, it still runs this function

Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)

How do I pop ViewControllerA?

sinusGob
  • 4,053
  • 12
  • 46
  • 82
  • Just because you present VC-B does not mean your VC-A stops running its timers. And you can't pop a viewcontroller that presents another viewcontroller. You are using "self.present" , self = VC-A , if VC-A presents something, and you want to pop it at the same time, how is that logical? You need to read tutorials and study around how everything works, so you can post a question that shows that you actually have atleast done your homework. Giving you a solution wont teach you or do you any good if you have not studied how things works, rather the opposite. You got an answer here anyways. GL –  Sep 02 '17 at 13:54
  • @Sneak What should I read then? and what should I do to stop the timer on the View ControllerA? – sinusGob Sep 02 '17 at 13:57
  • You need to read on how UIViewControllers works. Or the methods you are calling/coding atleast, i.e. present. https://developer.apple.com/documentation/uikit/uiviewcontroller/1621380-presentviewcontroller and your timer: https://developer.apple.com/documentation/foundation/timer/1415405-invalidate . You can for example invalidate your timer before you present (as someone answered below) . Or you can invalidate the timer on viewDidDissapear https://developer.apple.com/documentation/uikit/uiviewcontroller/1621477-viewdiddisappear . Just google the mehods you will find all the answers –  Sep 02 '17 at 14:00
  • also make sure you invalidate your timer before you pop your VC-A if you ever do so, having a timer running in a loop and popping the VC without invalidating the timer will keep your VC-A running in the background until the timer stops, which will keep a retain cycle https://stackoverflow.com/questions/3478361/best-time-to-invalidate-nstimer-inside-uiviewcontroller-to-avoid-retain-cycle –  Sep 02 '17 at 14:05

1 Answers1

0

Try this code -

class ViewControllerA: UIViewController {
     var timer : Timer!
     override func viewDidLoad() {
        super.viewDidLoad()
        // it runs this function every 5 seconds
        timer = Timer.scheduledTimer(5, target: self,selector: #selector(ViewControllerA.printNumber), userInfo: nil, repeats: true)
    }

    @IBAction func callViewControllerBButtonClicked(_ sender: UIButton) {
         if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerBID") as? ViewControllerB {
            timer.invalidate()
            self.present(vc, animated: true, completion: nil)
        }

    }

    func printNumber() {
      print(0)
    }
}
Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31