0rwar
0rwar

Reputation: 33

Swift navigating between viewcontrollers

Im looking for a word, method or similar for this situation. Say that i'm for example have 3 viewcontrollers, a, b and c.

And then i'm navigating from a to b and finally from b to c. From c then I want to dismiss my viewcontrollers all the way back to a. How do i achive this programmatically?

Upvotes: 0

Views: 38

Answers (2)

Frankenstein
Frankenstein

Reputation: 16341

Easiest way is to use UINavigationController: From A -> B (this code in A):

navigationController?.pushViewController(ViewControllerB(), animated: true)

From B -> C (this code in B):

navigationController?.pushViewController(ViewControllerC(), animated: true)

From C -> A (this code in C):

navigatinoController?.popToRootViewController(animated: true)

Upvotes: 1

DoesData
DoesData

Reputation: 7047

If you're using a UINavigationController you can use popToRootViewController

import UIKit

class AViewController: UIViewController {}
class BViewController: UIViewController {}
class CViewController: UIViewController {}

let a = AViewController()
let b = BViewController()
let c = CViewController()

let nav = UINavigationController(rootViewController: a)
nav.pushViewController(b, animated: true)
nav.pushViewController(c, animated: true)

// Option 1
nav.popToRootViewController(animated: true)

// Option 2
nav.popToViewController(a, animated: true)

Upvotes: 2

Related Questions