Search code examples
iphoneobjective-ciosxibnsbundle

Cannot manually load xib from a view controller?


I'm making an app using the Blackboard SDK. It would really help if you could download the SDK and see if you have the same errors. Here is a link to the instructions, in case anyone wants to follow along -- not too many pages.

The instructions say

"Because the module is running within the launcher application, the MainWindow.xib file is the one for the launcher. Therefore, the module cannot have its own MainWindow.xib file, as it would create a conflict.

Further, NO views will be loaded automatically from a nib file when the module first launches. Therefore, all views that are loaded from nibs must be loaded manually in your code, and all nib files must follow the same naming conventions and be included in your resources files.

Originally, I tried to instantiate a view controller like so

   testVC = [[LMU_LAL_ViewControllerTest alloc] initWithNibName:@"LMU_LAL_ViewControllerTest" bundle:nil];

However, it caused a crash. The error message I received was

Could not load NIB in bundle: 'NSBundle </Users/mahir/Library/Application Support/iPhone Simulator/6.0/Applications/2ABAAF9D-5141-41B2-8EFB-51C3E047AD67/testLauncher.app> (loaded)' with name 'LMU_LAL_ViewControllerTest

Someone said the problem could be solved by downloading an earlier version of xcode, but that hasn't helped yet. Also, I changed the method from initWithNibName:bundle: to init and added this to the view controller subclass

- (void)loadView
{
[super loadView];

UINib *nib = [UINib nibWithNibName:@"LMU_LAL_ViewControllerTest" bundle:nil];
[nib instantiateWithOwner:self options:nil];
} 

Again, I receive a similar error, but this time on the line [nib instantiateWithOwner:self options:nil]

What am I doing wrong??

EDIT:

Here is an excerpt from what someone from the support team gave me.

All the resources used by your module, including any .xib files, MUST be included in the project of the LAUNCHER. This means that in the project that you are actually running to test the module, the launcher project, all of the resource files need to be included inside that project. An easy way to test if this is the case is to find your .xib files by looking on the left hand bar of Xcode in the launcher project. This will ensure that the resource files are actually getting included in the project. I have included a screenshot of what it should look like in the launcher project for Xcode.

enter image description here

This is what I currently have

enter image description here


Solution

  • From your response to Erfan's solution, it seems that the problem is simply that the file LMU_LAL_ViewControllerTest.xib is not in the Target's Build Phases. That means that the target (your program) will never see the file when it is building, even if you can see it in the navigator.

    Another way you can add a xib file to the target is by doing the following:

    • select your Project, at the top of the Navigator pane
    • select the Target, in the next pane to the right of the Navigator
    • open Copy Bundle Resources: you will probably not see LMU_LAL_ViewControllerTest.xib listed here, although it should be
    • select + and select the file from the pop-up window to add it

    If this doesn't work (it may not since it's the equivalent of Erfan's solution), you can try deleting the file from the project and adding it back in. When you delete, make sure to Move to Trash. When you copy it again into the project, in the pop-up window that comes up, make sure to check the proper boxes next to Add to Targets.

    If this file is missing from your Build Phases, you may find that other files are also missing and will trigger similar errors. You can check all the Build Phases, particularly Copy Bundle Resources for other resource files and Compile Sources for the implementation files of your objective-C classes.