0
votes

I am looking to build an app with two view controllers (VC1 and VC2) that each contain a timer. I would like for my user to be able to switch back and forth between these views and allow each timer to still run.

Currently, I have been successful in unwinding to either VC1 or VC2 (depending on which I set as the initial view controller), though I am having difficulty in successfully unwinding back and forth (i.e. from VC1 to VC2 back to VC1 and etc.)

I figured I might be able to accomplish this by initially segueing from VC1 to VC2 with a normal segue (from a button press), and though it will unwind back to VC1, I cannot get my app to unwind back to VC2 from here.

My two simple timer view controllers

Here is the code (for VC1):

@IBAction func unwindToSecondView(_ sender: UISwipeGestureRecognizer) 
{
    performSegue(withIdentifier: "unwindToSecond", sender: self)    
}

@IBAction func backFromSecondView(segue: UIStoryboardSegue) {
}

And for VC2:

@IBAction func segueToFirstView(_ sender: UISwipeGestureRecognizer)
{
    performSegue(withIdentifier: "unwindToFirst", sender: self)
}

@IBAction func backFromFirstView(segue: UIStoryboardSegue) {
}

I am really new to swift, and have found a couple interesting sources on here (How to unwind two view controllers and Swift Timer() will not update label after switching between views) but have not had much success otherwise -- would really appreciate any advice or ideas for different implementations. Thanks!!

3
think of a standard segue as "forward" and an unwind segue as "back". When you segue from vc1 to vc2 you step forward when you unwind back to vc1 you step back. Now, it doesn't make sense to try and take another step "back" and end up moving forward, which is what you are trying to do. The simplest approach may be a UITabBarController, otherwise you are going to build your own container view controller and switch the content.Paulw11

3 Answers

0
votes

Apple has published good example for that. I recommend always check Apple Developer Guide sites and source code. Your answer below. https://developer.apple.com/library/content/samplecode/SegueCatalog/Introduction/Intro.html

-1
votes

segue from A-B and from B-A you can just dismiss() and if you want to send data back from B-A you can create a delegate.