- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试制作一个自定义控件,使用描述的新 IB_DESIGNABLE 选项在 Interface Builder 中实时更新 here .
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect myFrame = self.bounds;
CGContextSetLineWidth(context, 10);
CGRectInset(myFrame, 5,5);
[[UIColor redColor] set];
UIRectFrame(myFrame);
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath;
plistPath = [bundle pathForResource:@"fileExample" ofType:@"plist"];
UIImage *tsliderOff = [UIImage imageNamed:@"btn_slider_off.png"];
[tsliderOff drawInRect:self.bounds];
}
当我在模拟器中运行时,我得到一个红色框,中间是我的图像(正如预期的那样):
但是当我尝试使用 Interface Builder 时,它只显示为一个红色框(中间没有图像):
当我调试它时:Editor->Debug Selected Views,它显示从包中加载的任何东西都是零。 plistPath 和 tsliderOff 都显示为 nil。
我确保 btn_slider_off.png 包含在 Targets->myFrameWork->Build Phases->Copy Bundle Resources 中。
知道为什么 Interface Builder 在编辑期间看不到 png 文件,但在运行时显示正常吗?如果我无法加载任何要渲染的图像,“创建在 Interface Builder 中呈现的自定义 View ”会有点受限...
根据 rickster 的解决方案进行编辑
rickster 向我指出了解决方案——问题是文件不在 mainBundle 中,它们在 [NSBundle bundleForClass:[self class]] 中;并且 [UIImage imageNamed:@"btn_slider_off.png"] 似乎自动使用了 mainBundle。
下面的代码有效!
#if !TARGET_INTERFACE_BUILDER
NSBundle *bundle = [NSBundle mainBundle];
#else
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
#endif
NSString *fileName = [bundle pathForResource:@"btn_slider_off" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];
[image drawInRect:self.bounds];
最佳答案
截至首次提出此问题时,创建 IB 可设计控件需要将其打包在框架目标中。您不必再这样做了 — 发布的 Xcode 6.0(及更高版本)也将从您的应用程序目标中预览 IB 可设计的控件。但是,问题和解决方案是一样的。
为什么? [NSBundle mainBundle]
返回当前运行的应用程序的主要包。当您从框架调用它时,您会根据正在加载您的框架的应用程序返回不同的包。当您运行您的应用程序时,您的应用程序会加载框架。当您在 IB 中使用该控件时,一个特殊的 Xcode 帮助应用程序会加载该框架。即使您的 IB 可设计控件在您的应用程序目标中,Xcode 也会创建一个特殊的帮助应用程序来运行 IB 内部的控件。
解决方案?改为调用 +[NSBundle bundleForClass:]
(或 Swift 中的 NSBundle(forClass:)
)。这将为您提供包含您指定的任何类的可执行代码的包。 (您可以在此处使用 [self class]
/self.dynamicType
,但请注意,对于不同包中定义的子类,结果会发生变化。)
如果您正在使用框架方法——这对某些应用程序可能很有用,即使 IB 可设计控件不再需要它——最好将图像资源与使用它们的代码放在同一框架中。如果您的框架代码希望使用加载框架的任何应用程序在运行时提供的资源,那么使其可由 IB 设计的最佳做法是伪造它。在您的控件中实现 prepareForInterfaceBuilder
方法,并让它从已知位置(例如框架包或 Xcode 工作区中的静态路径)加载资源。
关于objective-c - xcode 6 IB_DESIGNABLE- 不从 Interface builder 中的 bundle 加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24603232/
这个问题已经有答案了: Difference between $Bundle install and $Bundle update (2 个回答) 已关闭 9 年前。 bundle 之间有什么区别,
我正在尝试加载 Nib ,但不断收到以下错误: -[NSViewController initWithCoder:] could not instantiate an NSViewController
bundle有什么区别& bundler命令? bundle有什么区别& bundle install ? 如果没有区别,为什么有多个命令做同样的事情? 最佳答案 可执行文件 bundle & bun
我们有托管在应用程序中的单元测试。要加载测试资源,我们使用:Bundle(for: TestClass.self).path(forResource: "some-file", ofType: "js
我刚刚克隆了一个新的 repo 并尝试运行 bundle install但出现以下错误 Fetching gem metadata from https://abcderepos.net/api/ge
我添加了一个共享框架来在应用程序和 watch 扩展之间共享代码。后来我删除了共享框架,因为它会导致很多问题。我可以 build 并在 iphone 上运行我的应用程序并观看。然而,当我提交到应用商店
这个问题有点类似于 this one ,但不完全是。我有一个 C# 游戏引擎,我正在与一些想要使用我的引擎的人一起工作。最初我设计了引擎,以便所有 Assets 都是外部的——非程序员可以创建艺术、音
我正在尝试使用 OSGi 实现客户端-服务器模型。服务器应用程序是在计算机中运行的 OSGi 框架,客户端应用程序远程连接到其控制台并通过 Java 套接字发送命令并接收正确的响应。每个客户端应用程序
我目前正在将我的 Angular 2 应用程序与 WebPack bundle 在一起。我们仍在快速循环,因此我们不想在构建和应用程序加载过程中增加延迟,而是希望包括很少更改的 Angular 2 U
基本上,我有一个捆绑软件,经常在其他 View 中使用,加上其他js文件,因此我可以在保留其顺序的同时将这些文件添加到现有捆绑软件中吗? 最佳答案 我到处都在查找它,但找不到将两个捆绑软件合并在一起的
我有一个大约 12GB 的巨大 mercurial 存储库。我需要在另一台机器上克隆它,但是从网络中提取它需要花费很多时间。当我尝试将所有变更集 bundle 到一个 bundle 文件中时,文件的大
我可以使用 Sonata User Bundle 将 FOS 包集成到 sonata Admin 包中。我的登录功能正常。现在我想添加 FOSUserBundle 中的更改密码等功能到 sonata
如果我检查使用 angular-cli 创建的 angular 2 项目的 index.html 文件,我可以看到该页面仅包含 dist 文件夹中的 3 个文件: inline.bundle.js v
我从程序包管理器http://localhost:4502/crx/packmgr/index.jsp中从正在运行的AEM实例下载了一个zip文件。提取后的zip文件包含jcr_root和META-I
已经提出了有关捆绑名称和捆绑显示名称的类似问题,例如: What's the difference between "bundle display name" and "bundle name" in
我正在尝试在 iTunes 上上传我的应用程序。为此,我创建了一个应用程序 ID 并保留了一个包标识符。在我的项目中,我更改了 info.plist 文件中的包标识符。但是,当我尝试在 itunes
我想从 OSGI 包启动 OSGI 包。正如您所看到的,此代码通过从目录部署它来启动 bundle : private void installStartBundle(BundleContext bc
所以这真的让我头疼,我终于放弃了,在这里发表了问题。我正在尝试更新iTune商店中的一个客户端应用程序,并且在上传到App Store时遇到以下错误。 因此,我已经尝试通过两次使用包sid id创建新
我在 typescript 中使用 aurelia,我想避免使用像这样的相对导入路径: import { DialogBox } from '../../resources/elements/dial
有什么区别 ResourceBundle.getBundle("Bundle") 还有这个 ResourceBundle.getBundle("/Bundle") 最佳答案 来自the Java do
我是一名优秀的程序员,十分优秀!