我正在尝试使用此处描述的新IB_DESIGNABLE选项创建一个在Interface Builder中实时更新的自定义控件.
- (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];
}
Run Code Online (Sandbox Code Playgroud)
当我在模拟器中运行时,我得到一个红色的盒子,我的图像在中心(正如预期的那样):

但是当我尝试使用Interface Builder时,它只显示为一个红色框(中间没有图像):

当我调试它:Editor-> Debug Selected Views时,它显示从bundle中加载的任何内容都是nil.plistPath和tsliderOff都显示为nil.
我确保btn_slider_off.png包含在Targets-> myFrameWork-> Build Phases-> Copy Bundle Resources中.
知道为什么Interface Builder在编辑期间没有看到png文件,但在运行时显示正常吗?如果我无法加载任何图像来渲染,"创建在Interface Builder中渲染的自定义视图"有点受限制...
根据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 = …Run Code Online (Sandbox Code Playgroud) UIDocumentInteractionController presentOptionsMenuFromBarButtonItem 在ios8硬件中给出了一个控制台错误(而不是7.1硬件或更早版本):
Unknown activity items supplied: (
{
"com.adobe.pdf" = ;
},
""
)
在我的应用程序的官方App Store版本中,应用程序崩溃了.当我在iPad上编译并运行时,它只是给出错误但不会崩溃.
我的代码:
在.h:
UIDocumentInteractionController *docInteractionController;
Run Code Online (Sandbox Code Playgroud)
在他们中:
self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
self.docInteractionController.delegate = self;
//UIBarButtonItem *element is an element in my toolbar
[self.docInteractionController presentOptionsMenuFromBarButtonItem:element animated:YES];
Run Code Online (Sandbox Code Playgroud)
如果我执行NSLogdocInteractionController.UTI,我会在控制台看到"com.adobe.pdf",因此正确识别UTI.
我可以通过使用presentOpenInMenuFromBarButtonItem而不是presentOptionsMenuFromBarButtonItem为UIDocumentInteractionController调用来解决"未知活动项目",但我想向用户显示打印和电子邮件选项,而不仅仅是外部应用程序打开选项.
在iPad 8.0.2版上测试过.Xcode版本6.0.1,部署目标6.0(也使用部署目标8.0进行测试).所有目标-c.在iPad 7.1版上运行不会产生错误.