2010-11-08 52 views
2

我有一個nib文件說TestView.xib。我添加了一些組件。通過Interface Builder在另一個視圖中添加一個帶有nib文件的現有視圖

現在我創造了另一個榫文件AnotherTestView.xib

我想用接口buidler,所以我不需要從TestView再次添加同一部件TestView.xib的視圖來添加到AnotherTestView.xib。它們就像我所有觀點的基本組成部分。

有沒有辦法做到這一點。就像我們可以在IB中設置UIView的nib文件名。或者,我們如何將這個具有nib文件的現有視圖添加到另一個nib文件中。

+0

這正是我有同樣的問題。卡住了,完全失去了! – haroldcampbell 2010-12-09 02:41:54

回答

0

我寫了一篇很長的博客文章,我們如何embed custom-view Nibs inside other Nibs。關鍵是在您的自定義視圖中重寫-awakeAfterUsingCoder:,用從「嵌入」Nib(TestView.xib)加載的對象替換從AnotherTextView.xib加載的對象。

這些方針的東西:

// TestView.m 
- (id) awakeAfterUsingCoder:(NSCoder*)aDecoder { 
    BOOL theThingThatGotLoadedWasJustAPlaceholder = ([[self subviews] count] == 0); 
    if (theThingThatGotLoadedWasJustAPlaceholder) { 
     // load the embedded view from its Nib 
     TestView* theRealThing = nil; 
     NSArray* elements = [[NSBundle mainBundle] loadNibNamed: NSStringFromClass([TestView class]) 
                  owner: nil 
                 options: nil]; 
     for (id anObject in elements) { 
      if ([anObject isKindOfClass:[TestView class]]) { 
       theRealThing = anObject; 
       break; 
      } 
     } 

     // pass properties through 
     theRealThing.frame = self.frame; 
     theRealThing.autoresizingMask = self.autoresizingMask; 

     [self release]; 
     self = [theRealThing retain]; 
    } 
    return self; 
} 
相關問題