Ios 如何使用IB_DESIGNABLE在故事板中可视化可重用的XIB?
我想使用Xcode 9.2中的IB_可设计常量,在故事板中可视化XIB,目标C针对iOS SDK 11.2。 根据苹果公司的说法,Interface Builder调用了Ios 如何使用IB_DESIGNABLE在故事板中可视化可重用的XIB?,ios,xcode,interface-builder,xcode9.2,Ios,Xcode,Interface Builder,Xcode9.2,我想使用Xcode 9.2中的IB_可设计常量,在故事板中可视化XIB,目标C针对iOS SDK 11.2。 根据苹果公司的说法,Interface Builder调用了PrepareforPrinterFaceBuilder方法,让您只为Interface Builder做任何事情 当Interface Builder使用IB_DESIGNABLE属性实例化一个类时,它会调用此方法,让生成的对象知道它是在设计时创建的。您可以在可设计类中实现此方法,并使用它配置它们的设计时外观 基于此,我想在我
PrepareforPrinterFaceBuilder
方法,让您只为Interface Builder做任何事情
当Interface Builder使用IB_DESIGNABLE属性实例化一个类时,它会调用此方法,让生成的对象知道它是在设计时创建的。您可以在可设计类中实现此方法,并使用它配置它们的设计时外观
基于此,我想在我的故事板中可视化XIB。当我在ViewController上使用UIView
元素并设置
- 首先将自定义类设置为
XibView
- 将
属性添加到nibName
,即XIB文件的名称MainViewController
Main.storyboard:
error: IB Designables:
Failed to render and update auto layout status for MainViewController (8zi-kg-fjR):
The agent threw an exception.
在Main.storyboard中使用的视图中,我得到了这个错误,而不是可视化的XIB,这是我做错了什么
这是XibView.h
IB_DESIGNABLE
@interface XibView : UIView
@property (nonatomic, strong) IBOutlet UIView* contentView;
@property (nonatomic) IBInspectable NSString* nibName;
@end
这是XibView.m
#import "XibView.h"
@implementation XibView
- (id)initWithCoder:(NSCoder *) coder
{
self = [super initWithCoder: coder];
if (self)
{
[self xibSetup];
}
return self;
}
//---------------------------------------------------------------------
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self xibSetup];
}
return self;
}
//---------------------------------------------------------------------
- (void)prepareForInterfaceBuilder
{
[super prepareForInterfaceBuilder];
[self xibSetup];
}
//---------------------------------------------------------------------
- (void)xibSetup
{
[[NSBundle mainBundle] loadNibNamed:_nibName owner:nil options:nil];
[self addSubview:self.contentView];
self.contentView.frame = self.bounds;
}
@end
以下是我认为有缺陷的方法:
- (void)xibSetup
{
[[NSBundle mainBundle] loadNibNamed:_nibName owner:nil options:nil];
[self addSubview:self.contentView];
self.contentView.frame = self.bounds;
}
nib加载程序无法设置self.contentView
,因为您没有将self
作为owner
参数传递给loadNibNamed:options:
。请尝试以下方法:
[[NSBundle mainBundle] loadNibNamed:_nibName owner:self options:nil];
在我的帮助下,我还是基于这个发现了问题的所在。要提供答案供其他人使用,您可以在此处找到步骤
如何使用Objective-C可设计的IB_在故事板中可视化可重用的XIB?
XibViewHolder
)MyView.Xib
)XibViewHolder
UIView
UIView
的类设置为XibViewHolder
类UIView
接口生成器属性设置为要加载的Xib的名称,例如:MyView
XibViewHolder.h
IB_DESIGNABLE
@interface XibViewHolder : UIView
@property (nonatomic, strong) UIView* contentView;
@property (nonatomic, strong) IBInspectable NSString* nibName;
@end
这是XibViewHolder.m
#import "XibViewHolder.h"
@implementation XibViewHolder
- (void)awakeFromNib
{
[super awakeFromNib];
[self xibSetup];
}
- (void)prepareForInterfaceBuilder
{
[super prepareForInterfaceBuilder];
[self xibSetup];
[self.contentView prepareForInterfaceBuilder];
}
- (void)xibSetup
{
if (_nibName == nil) { return; }
UIView* loadedNibView = [self loadViewFromNib];
loadedNibView.frame = self.bounds;
loadedNibView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self addSubview:loadedNibView];
self.contentView = loadedNibView;
}
- (UIView*)loadViewFromNib
{
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
UINib* nib = [UINib nibWithNibName:_nibName bundle:bundle];
return [nib instantiateWithOwner:self options:nil].firstObject;
}
@end
跳出的第一件事。。。。您实现了
prepareforninterfacebuilder
,在其中调用prepareforninterfacebuilder
。。。无限循环?删除它并更新问题。这是我做的一次测试。使用swift怎么样?@E.com谢谢你的提示。这将如何帮助我解决这个问题?我尝试了swift。它也不起作用。也许你需要其他方法来实现你的目标。别再这样了谢谢你的回答。我将owner
设置为self
,但没有效果。还是相同的错误。您是否也连接了xib中的contentView
插座?不,我为什么要这样做?据我所知,contentView保存加载的xib。如果不连接xib中的插座,为什么您认为contentView
保存加载的xib?您是否尝试过在代码中放置断点,然后在xib中使用编辑器>调试所选视图?