简体   繁体   中英

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.

在此处输入图片说明

This is what I currently have

在此处输入图片说明

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.

You'll find the explanation for that in the documentation of initWithNibName:bundle:

The bundle in which to search for the nib file. This method looks for the nib file in the bundle's language-specific project directories first, followed by the Resources directory. If nil , this method looks for the nib file in the main bundle.

As for the error, you probably have a typo in the xib file's name or in your code.

I downloaded the Blackboard SDK you linked to, and successfully added a view controller with a .xib file to the ExampleModule and launched it from the outer SpringboardApplication .

The core action you need is to place compiled versions of your .xib file into the output folder . I'll show how to do this below. This is based on what CocoaPods does.

  1. Create a view controller in your module subproject, as shown below.

    XCode应用程序文件列表

  2. Create a shell script in your Springboard Application project as shown above, using the file contents listed below.

  3. Mark the script as executable! Otherwise it will not run. This can be done from the commandline:

    chmod +x /path/to/your/script.sh

  4. Open the Build Phases tab of your Springboard Application . Add a Run Script build phase, and reference the script as shown below.

    XCode应用程序的“构建阶段”选项卡

This script will use ibtool to create compiled versions of your .xib files and place them into your compiled application, such that the application can reference them.

If you do not have control of the SpringboadApplication, you may not be able to use XIB files because static libraries can ONLY contain compiled object code, and not resources or bundles. If the owner of the SpringboardApplication is willing to work with you, a variation of my answer here or this StackOverflow question may work for you.

Here's the contents of that script, based on the CocoaPods script (MIT License):

#!/bin/sh

install_resource()
{
    case $1 in
        *.storyboard)
            echo "ibtool --errors --warnings --notices --output-format human-readable-text --compile ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/`basename $1 .storyboard`.storyboardc ${SRCROOT}/$1 --sdk ${SDKROOT}"
            ibtool --errors --warnings --notices --output-format human-readable-text --compile "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/`basename $1 .storyboard`.storyboardc" "${SRCROOT}/$1" --sdk "${SDKROOT}"
            ;;
        *.xib)
            echo "ibtool --errors --warnings --notices --output-format human-readable-text --compile ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/`basename $1 .xib`.nib ${SRCROOT}/$1 --sdk ${SDKROOT}"
            ibtool --errors --warnings --notices --output-format human-readable-text --compile "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/`basename $1 .xib`.nib" "${SRCROOT}/$1" --sdk "${SDKROOT}"
            ;;
        *)
            echo "cp -R ${SRCROOT}/$1 ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
            cp -R "${SRCROOT}/$1" "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
            ;;
    esac
}
install_resource `ls ../../Modules/ExampleModule/*.xib`

Notice the last line must be customized to include all resources that need to come from your Module into the Application. Obviously this can be accomplished using multiple lines.

I did not alter the ViewController code at all—it remained as it was from the XCode generated version.

If you are not able to make this work on the ~/Blackboard Mobile SDK/Samples/Application/SpringboardApplication project, let me know and I'll provide more details if needed.

Select the NIB file that is causing the error (LMU_LAL_ViewControllerTest.xib in your case), watch out on the file inspector and go to the "Target Membership" section. There you will see your project name with a check box at it's left. Check that box (mark as selected) and build & run your application. It happens to me before and i've resolved that in this way. You may have a try this and let me know if it helps you or not.

The fact that you don't specify a bundle, does not mean it will not be loaded from a "bundle", it just means that it will be loaded from the application bundle which is the default. I would make sure the name in NibName matches your Xib file in Interface Builder.

Try specifying the extension. Try loading @"LMU_LAL_ViewControllerTest.xib"

Please got to Application Build setting and in Bundle resources check that .xb file is added or not If not added then ad it. It will resolve ur problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM