2012-07-10 26 views
1

我試圖讓在Xcode的界面生成器可可的應用程序接口。我在「接口」構建器中創建的其中一個窗口的視圖取決於來自其他視圖的數據,因此必須從必須傳遞其數據的類向消息傳遞此視圖。不過,我似乎無法找到一種方法從nib文件的所有者獲取此視圖對象的引用。這裏大致是我正在使用的代碼:如何從interfacebuilder訪問窗口的視圖?

controller = [[NSWindowController alloc] initWithWindowNibName:@"Somenibname"]; 
[[controller window] display]; 
theOtherView = [[[[controller window] contentView] subviews] objectAtIndex:1]; 
[theOtherView setObjectwhichneedstobemessaged:self]; 
[theOtherView sendAMessage:self]; 

此代碼所在的對象從不接收消息。最初實際上我認爲內容查看是出現在界面生成器的視圖,並試圖獲得對它的引用這樣

theOtherView = [[[控制器窗口]內容查看]];

但也不能工作。謝謝閱讀。

回答

1

這聽起來像你可能需要一個更好的瞭解視圖 - 控制器結構如何與筆尖文件,並沒有更多的代碼/細節工作,其難度確切地知道你正在嘗試做的,但一個快速的方法來解決你的問題可能是使用NSNotification而不是試圖找到其他視圖並通過調用鏈發送消息。

可以註冊以處理從接收視圖的通知和(如果你需要它兩種方式,反之亦然)發送從策動視圖的通知。

1

閱讀上IBOutlet,並考慮把你直接從界面生成器所需要的意見。

例如,您NSWindowController子類可能有:

@interface MyWindowController : NSWindowController 
{ 
    /* can also use more specific classes if you need them, e.g. NSButton if it's really an NSButton */ 
    IBOutlet NSView* firstViewIWant; 
    IBOutlet NSView* secondViewIWant; 
} 
    . . . 
@end 

您的實現可以有:

- (void) 
windowDidLoad 
{ 
    [super windowDidLoad]; 
    /* make sure the views were connected properly */ 
    assert(nil != firstViewIWant); 
    assert(nil != secondViewIWant); 
    . . . 
} 

然後在Interface Builder中,掛鉤從 「文件的所有者」 這些出口的確切意見你需要他們。