2015-07-01 73 views
0

我在xib中设计了自定义视图。我有CustomView.h/m + CustomView.xib。在xib文件中,我将视图类设置为CustomView。Ios:在xib中加载自定义视图

现在我想在其他xib中使用这个视图。我创建了另一个xib,添加了视图并将其设置为CustomView,但未加载。

如何正确加载我的CustomView?

+0

阅读它会帮助你 http://stackoverflow.com/questions/5095359/using-xib-object-inside-another-xib – Omarj

+0

@Omarj提供的答案提供了无限循环,你不能在initWithCode方法中调用loadNib方法 – user1284151

+0

更准确地说,你如何加载'CustomView'以及究竟发生了什么?你在推一个新的'UIViewController'吗? – SwiftArchitect

回答

2

简答:是的。

您可以使用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) 
     } 
    } 

参见:

+0

,我问:如何加载它:) – user1284151

+0

在那个问题视图是从代码加载,而不是从xib/storyboard – user1284151

1

您需要使用IB_Designable关键字来使您的自定义视图在IB中正确显示。但是在nib中设计自定义视图而不是重写drawRect使其更加涉及。退房this link看你如何能做到这一点: