Frederik Norlyk Eriksen Frederik Norlyk Eriksen - 1 year ago 69
Swift Question

Accessing a UIViewController using storyboardID without instantiating it

Is there any way to access a

without using the function
instantiateViewController(withIdentifier: String)
. I have to call the function
from within another
which I have transitioned to using a show segue and I therefore do not want to instantiate the initial


Answer Source

You can access through UINavigationController stack. Let say if it is one controller before, you can access it using the total viewcontrollers (in stack) minus 2

let previousController = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.count)! - 2] as! YourViewController

more simplified

let controllerIndex = (self.navigationController?.viewControllers.count)! - 2
let previousController = self.navigationController?.viewControllers[controllerIndex] as! YourViewController
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download