Frederik Norlyk Eriksen Frederik Norlyk Eriksen - 1 month ago 7
Swift Question

Accessing a UIViewController using storyboardID without instantiating it

Is there any way to access a

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

Thanks.

Answer

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
Comments