Why is my application delegate didFinishLaunchingWithOptions completely unexpectedly called AFTER my RootViewController: viewDidLoad method?

I played with the iPad SplitView template in Xcode. Here are two of the many important methods that are automatically generated for you using the Split View-based application template ...

AppNameAppDelegate.m

#pragma mark - #pragma mark Application lifecycle - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after app launch rootViewController.managedObjectContext = self.managedObjectContext; // Add the split view controller view to the window and display. [window addSubview:splitViewController.view]; [window makeKeyAndVisible]; return YES; } 

RootViewController.m

 #pragma mark - #pragma mark View lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.clearsSelectionOnViewWillAppear = NO; self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } 

When you create and run a project before any changes, the application:didFinishLaunchingWithOptions is called before the RootViewController:viewDidLoad method RootViewController:viewDidLoad . I am new to iPhone development, but I guess this is the correct and typical sequence. So, here are the changes I made ...

  • As soon as I confirmed that everything works without any changes, I changed the RootViewController code and set it as a subclass of the UIViewController (instead of the default UITableViewController ) and made the appropriate settings in Interface Builder. I built and ran, everything was fine.
  • Then I added a UIView (without anything) to the RootView in IB, and when I built and started it, suddenly RootViewController:viewDidLoad is called before the application:didFinishLaunchingWithOptions .

I need to get it back to the way it worked before, because as you can see in the code, the viewDidLoad method depends on the didFinishLauchingWithOptions method, so it can set the rootViewController managedObjectContext that it uses to perform the selection.

  • Any ideas what caused this?
  • Any ideas how I can fix this?

Thank you for your help! I will continue to study and play with the code.

+6
xcode ipad interface-builder
source share
3 answers

In the template app -applicationDidFinishLaunching adds the RootViewController view to the window, causing the view to load, so obviously -viewDidLoad - applicationDidFinishLaunching will be displayed.

ViewDidLoad (indirectly) is called from applicationDidFinishLaunching.

If, as you say, viewDidLoad is called before applicationDidFinishLaunching, it is because you did something to make the view load before calling applicationDidFinishLaunching.

Have you added a breakpoint to -viewDidLoad and looked at the stack to find out what is responsible for calling it?

+4
source share

Where do you initialize the RootViewController ? Typically, you do this in applicationDidFinishLaunching (at least on the iPhone). If you initialize it in the init delegate method of your application, this can lead to a call to the root view control method viewDidLoad before applicationDidFinishLaunching.

+2
source share

These are failures caused by the fact that in MainWindow.xib your delegated application object is not associated with a file owner (UIApplication). You can open MainWindow.xib and right-click your application delegate to find out if it has a connection to the "Backup Output File" file owner. If not, install it. And that will fix your problem.

+2
source share

All Articles