LK Phuc Nguyen LK Phuc Nguyen - 6 months ago 13
Swift Question

Swift - Access different View Controllers in App Delegate

In

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
didFinishLaunchingWithOptions
in
app delegate
that I can reference the first screen as
viewController[0]
:

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!

Answer

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

self.presentViewController(referencedViewController,
 animated: true, completion: nil)
Comments