10

可能重复:
您可以从 iPhone 上的静态库中引用 Xib 文件吗?

我创建了一个 iOS 框架,它包含一个 xib 视图。当我调试程序时,这行代码运行良好:

MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];

但是,当我从另一个项目中“引用”框架时 - 笔尖不再位于“mainBundle”中。我应该如何处理上面的代码(这是框架的一部分),以便它从框架而不是消费应用程序项目加载?

4

1 回答 1

19

The framework in question should have a bundle identifier in its Info.plist file typically. In the application that makes use of this custom framework, you should be able to access resources in this bundle by:

NSString *frameworkBundleID = @"com.yourCompany.YourFrameworkBundleID";
NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:frameworkBundleID];

This is the NSBundle from which you can access framework resources.

EDIT:

There appears to be an alternate (possibly better) means of accessing the bundle in question.

e.g.:

NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"MyLibraryResources" withExtension:@"bundle"]];

See this excellent tutorial

于 2012-09-24T02:10:54.680 回答