我在xib中设计了自定义视图。我有CustomView.h/m
+ CustomView.xib
。在xib文件中,我将视图类设置为CustomView。Ios:在xib中加载自定义视图
现在我想在其他xib中使用这个视图。我创建了另一个xib,添加了视图并将其设置为CustomView
,但未加载。
如何正确加载我的CustomView?
我在xib中设计了自定义视图。我有CustomView.h/m
+ CustomView.xib
。在xib文件中,我将视图类设置为CustomView。Ios:在xib中加载自定义视图
现在我想在其他xib中使用这个视图。我创建了另一个xib,添加了视图并将其设置为CustomView
,但未加载。
如何正确加载我的CustomView?
简答:是的。
您可以使用
CustomView.h/m
在许多.XIB
文件,您认为合适的,包括.storyboard
问题在于你如何加载第二个视图。
if let navigationController = self.navigationController {
let sb = UIStoryboard(name: "Main", bundle: nil)
if let vc = sb.instantiateViewControllerWithIdentifier("id") as? UIViewController {
navigationController.pushViewController(vc, animated: true)
}
}
参见:
,我问:如何加载它:) – user1284151
在那个问题视图是从代码加载,而不是从xib/storyboard – user1284151
您需要使用IB_Designable
关键字来使您的自定义视图在IB中正确显示。但是在nib中设计自定义视图而不是重写drawRect使其更加涉及。退房this link看你如何能做到这一点:
阅读它会帮助你 http://stackoverflow.com/questions/5095359/using-xib-object-inside-another-xib – Omarj
@Omarj提供的答案提供了无限循环,你不能在initWithCode方法中调用loadNib方法 – user1284151
更准确地说,你如何加载'CustomView'以及究竟发生了什么?你在推一个新的'UIViewController'吗? – SwiftArchitect