![](/img/trans.png)
[英]iPhone IOS5 Storyboard, how to load a UIViewController with a custom .xib file?
[英]how to load custom view with xib on a xib or storyboard
我只想在viewcontoller的xib或装饰板上加载自定义视图(使用xib)。最佳做法是这样做
我在下面使用了awakeAfterUsingCoder函数代码。
(id) awakeAfterUsingCoder:(NSCoder*)aDecoder
{
BOOL theThingThatGotLoadedWasJustAPlaceholder = ([[self subviews] count] == 0);
if (theThingThatGotLoadedWasJustAPlaceholder)
{
CustomView* theRealThing = (id) [CustomView view];
theRealThing.frame = self.frame;
theRealThing.autoresizingMask = self.autoresizingMask;
return theRealThing;
}
return self;
}
但是使用此功能后,我的awakeFromNib开始多次调用。
请萨格斯特。
如您所知,当前链接到的正确答案过于复杂和错误。 有一种更标准,更好的方法来执行此操作:
实例化XIB并检索您的CustomView实例,如下所示:
CustomView * view = [[[UINib nibWithNibName:@“ CustomView”捆绑包:无] instantiateWithOwner:self选项:无] [0];
通过视图控制器的viewDidLoad方法将其添加到视图层次结构中:
[self.view addSubview:view];
一定要重写-initWithCoder:
在情况下,你CustomView子类,你需要做的任何定制的初始化。 我知道很多事情,所以如果这些步骤使您感到困惑或卡住,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.