2015-07-01 68 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看你如何能做到這一點: