Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:14:29+00:00 2026-06-06T20:14:29+00:00

I think I have got something simple wrong here. I am trying to pass

  • 0

I think I have got something simple wrong here.
I am trying to pass the AppDelegate’s managedObjectContext to two view controllers.
Just for this learning demo, the two ViewControllers are right next to each other. (In my real app, they will be far apart.)

enter image description here

This works:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

    Page1ViewController * page1VC = [[navigationController viewControllers] objectAtIndex:0];
    page1VC.managedObjectContext = self.managedObjectContext;

    return YES;
}

But when I add the second view controller – objectAtIndex:1, it doesn’t:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

    Page1ViewController * page1VC = [[navigationController viewControllers] objectAtIndex:0];
    page1VC.managedObjectContext = self.managedObjectContext;

    Page2ViewController *page2VC = [[navigationController viewControllers] objectAtIndex:1];
    page2VC.managedObjectContext = self.managedObjectContext;

    return YES;
}

I get this error:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

I am sure it is close to being correct!? Would be grateful if someone could help.
(Btw, I will be using segues to pass managedObjectContext too!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:14:31+00:00Added an answer on June 6, 2026 at 8:14 pm

    A navigation controller will not instantiate all of the view controllers in it’s navigation tree when it gets instantiated; but instead only when they are actually about to come on screen. This is a very good thing for memory management.

    As you mentioned, you can easily use the prepareForSegue to pass around an MOC.
    An alternative solution that I have used extensively, is to create a helper class to manage MOC instances for you, which you can use from anywhere. It manages creating custom contexts, and also handles thread safety (never use an MOC on a thread which did not create it! You may run into this issue if you simply pass one MOC around freely)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a simple TPH example from http://msdn.microsoft.com/en-us/library/dd793152.aspx . I have two
I've got a simple question that I just KNOW won't be possible, but here
I have studied generators feature and i think i got it but i would
I think I have a big problem. I have a two projects solution. First
I think I have a basic understanding of REST, but something I'm stuck on
I have a quite simple WCF service method which returns an IQueryable, just for
I have profiled for , while and do-while loops with something simple: while ($var
I think I've got what sounds like a simple request but is probably actually
Say that I have simple command line application, that looks something like this #
I have a feeling I've missed something obvious here, but I don't know where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.