0

This code illustrates what I am asking:

For a simple app having 2 screens, the rootViewController and a TableViewController, the rootVC will be at index [0] in navigationController, the next views will increments the index by 1

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


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

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

        return true
    }

Now I am trying to pass managedObjectContext from appDelegate to the ViewControllers behind the rootViewController:

The center view is rootViewController, I want to pass managedObjectContext to the screens next, above and below it.

I have tried passing it from appDelegate to rootViewController, then passing from rootViewController to the next views but it does not work.

Anyone please tell me how to know the index numbers of the views following the same view? Thank you!

Jay Nguyen
  • 342
  • 1
  • 2
  • 18
  • To determine the index of the current view in the navigation controller's stack, simply use `self.navigationController?.viewControllers.count - 1`. Not clear from you question why you would need this though. – shim May 21 '16 at 21:38

2 Answers2

1

Now I am trying to pass managedObjectContext from appDelegate to the ViewControllers behind the rootViewController

You can't. The entire idea makes no sense. At the time the code in the app delegate runs, those view controllers do not exist yet. A storyboard is merely a set of instructions for making future instances of view controllers; there are no such instances during application:didFinishLaunchingWithOptions: — except for the first one that you make, explicitly, right there in your code.

Instead, each of those view controllers needs to fetch the managedObjectContext from the app delegate, in their own code (such as their viewDidLoad). That is something they can all easily do, because the app delegate has a managedObjectContext property, and they can all see that property because they can all see the app delegate (because it is the shared application's delegate).

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

I don't quite agree with the answer above owing to the fact that it's considered a bad practice getting hold of your app delegate from your view controllers or any other object for that matter just to get it's stored properties.

What you can do is to use the concept of dependency injection to pass the managedObjectContext along your view controllers as you move down your view hierarchy using the prepareForSegue method if you are performing a segue or assigning it to the next view controller if you are instantiating and pushing it on your navigation stack programmatically.

    // App delegate: - didFinishLaunchingWithOptions
    let navCon = window?.rootViewController as? UINavigationController
    let rootVC = navCon?.viewControllers.first as? firstViewController
    rootVC?.managedObjectContext = managedObjectContext

    // FirstViewController: - Using segue
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      guard let identifier = segue.identifier else { return }
      switch identifier {
      case "SecondView":
        let navCon = segue.destinationViewController as? UINavigationController
        let controller = navCon?.topViewController as? SecondViewController
        controller?.managedObjectContext = self.managedObjectContext
      default:
        break
      }
    }

    // FirstViewController: - Pushing programmatically 
    let controller = SecondViewController()
    controller.managedObjectContext = self.managedObjectContext
    navigationController?.pushViewController(controller, animated: true)
Ahmed Onawale
  • 3,992
  • 1
  • 17
  • 21