开发者

How do I use InterfaceBuilder files in Xcode subprojects?

I'm developing an iPhone app that is a "module" of another launcher (it doesn't launch from the iPhone home screen). To add this module to the launcher, I have to drop in the xcode file into the parent xcode project (creating a subproject). The subproject uses a NIB file as its view controller and the subproject loads the file using initWithNib:

root_view_c开发者_JAVA技巧ontroller = [[UINavigationController alloc] initWithRootViewController:[[LMU_IP_RootView alloc] initWithNibName:@"LMU_IP_RootView" bundle:nil]];

When I try to run the parent project, it crashes with:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle [...] (loaded)' with name 'LMU_IP_RootView''

I'm guessing its because it can't find the NIB file because the root bundle is now the parent project instead of the subproject. I could include the NIB in the parent project and that fixes the error, but doesn't solve my problem.

So my question: How do I use InterfaceBuilder files in a subproject? Do I have to specify a bundle? How do I specify a bundle that refers to this subproject?

Thanks!


When bundle is nil, the search is performed in the main bundle, not in your subproject bundle. I'm not entirely clear on your app architecture, but try specifying the bundle containing the class as the bundle to search:

NSBundle *classBundle = [NSBundle bundleForClass:[LMU_IP_RootView class]];
id vc = [[LMU_IP_RootView alloc] initWithNibName:nil bundle:classBundle]
root_view_controller = [[UINavigationController alloc]
                        initWithRootViewController:vc];
[vc release];

Note that a nil nib name defaults to "ClassName.nib", so LMU_IP_RootView.nib will be searched for in this case.

It's generally better to override -init in your view controller class so it performs the correct lookup. Then client code doesn't have to worry about what nibs the class needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜