[英]windowControllerDidLoadNib is not getting called in OSX app
我注意到我的OSX應用程序(基於文檔)的行為很奇怪,並且我感覺自己在這里做錯了。
當我從Xcode運行我的應用程序時,它運行良好,並且一切正常。 如果我按Command + Q退出應用程序,那一切都很好。
當我通過按Xcode中的“停止”按鈕退出應用程序時,該應用程序會像通常那樣停止運行,但是當我下次運行它時,直到我單擊停靠菜單中的應用程序圖標時,我的windowControllerDidLoadNib才會被調用(盡管該應用程序正在運行,我檢查了活動監視器。
文件
- (NSString *)windowNibName
{
return @"Document";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController
{
[super windowControllerDidLoadNib:windowController];
}
我真的對這種行為感到困惑,我不知道自己在做錯什么,或者這是否應該如此。
任何幫助都是高度贊賞的
只是您必須在documentcontroller的methof下面調用以加載nib方法
NSDocumentController * docC=
[[yourDocumentCont alloc]init];
[docC openDocument:self]
需要窗口控制器時將調用windowControllerDidLoadNib。 當您單擊停靠圖標時,它將打開一個新文檔或創建一個新文檔,這是一種系統樣式。 應用程序在后台運行時無需窗口,因此我認為沒有錯。
僅在以下情況下,在NSDocument子類中調用windowControllerDidLoadNib:
windowController的所有者是XIB的文檔。 在XCode中,XIB所有者定義了NSDocument子類,而不是NSWindowController子類
您重寫windowControllerDidLoadNib方法
從NSWindowController的實現中:
...
[self windowDidLoad]
if ([self owner] != self) {
if ([self respondsToSelector:@selector(windowControllerDidLoadNib:)]) {
[[self owner] windowControllerDidLoadNib:self];
}
}
...
如果您需要這種行為,只需將此代碼添加到您的NSWindowController子類中
- (void)windowDidLoad {
[super windowDidLoad];
if ([self owner] == self && [self document]) {
if ([[self document] respondsToSelector:@selector(windowControllerDidLoadNib:)]) {
[[self document] windowControllerDidLoadNib:self];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.