-1

I have 8 tabs in an RSS News Reader. Each tab links to one navigation controller, which links to one masterViewController containing the table view. I am trying to loop through all the tabs to preload their table data at launch. I tried the code below in my appDelegate didFinishLaunchingWithOptions method:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [navViewController.topViewController view];
}

However, whenever I launch the Application, I get the following error message:

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

What is the problem here?

somethingstrang
  • 1,079
  • 2
  • 14
  • 29
  • That's strange… That error happens when you call an index beyond what's currently in the array. But since you're doing the for loop without indices, it doesn't make sense that it's coming from that for loop… Are you sure it's from this section of code? – Lyndsey Scott Mar 07 '14 at 03:20
  • Oh, just thought of a possible reason… one sec... – Lyndsey Scott Mar 07 '14 at 03:25

1 Answers1

0

I suspect the problem may be that

NSArray *myViewControllers = tabBarController.viewControllers;

is returning an array of UIViewControllers (as I'd assume it should), but your for loop is looking for UINavigationControllers.

I don't know what you're trying to do with that loop since you don't assign or use [navViewController.topViewController view]; for anything, but if you want to access the views of the view controller in your myViewControllers array, use:

for (UIViewController *viewController in myViewControllers)
{
    variableWhatever = [viewController view];
}
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • I am following this post: [link](http://stackoverflow.com/questions/9202737/load-all-tabbar-views), in order to preload the table data of all my tabs on application launch. Their solution doesn't work however, so I thought to call "view" on my masterViewController instead of the navigationController. Your solution eliminates the crash, but still doesn't cause my tabs to preload. – somethingstrang Mar 07 '14 at 16:26
  • So in summary, I am just trying to loop through all my masterViewControllers, and calling view on them to force loading of table data for each tab. – somethingstrang Mar 07 '14 at 16:31