LK Phuc Nguyen LK Phuc Nguyen - 5 months ago 6x
Swift Question

Swift - Access different View Controllers in App Delegate


app delegate
, with a simple app having only 2 screens:

  • first screen is a Table View Controller embedded in Navigation Controller

  • second screen is a View Controller which is used to add items to the first screen table via protocol/delegate/segue

And this is the code for
app delegate
that I can reference the first screen as

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let navController = self.window?.rootViewController as! UINavigationController
let courseListController = navController.viewControllers[0] as! CourseListController
courseListController.managedObjectContext = self.managedObjectContext

return true

How can I reference the screens at above, below and next to the center sreen? Please suggest me a solution. Thank you!


Go to each view Controller you want to reference and in the identity inspector, add some string to its StoryBoard ID.

next to reference it from the new ViewController (say, XViewController) to (say, YViewController) do this :

var referencedViewController = self?.storyboard.
instantiateViewControllerWithIdentifier("referenceViewID") as! YViewController

 animated: true, completion: nil)