2011-12-02 73 views
0

我在XCode4中編寫了一個Cocoa/Objective-C應用程序,並且我需要知道我的首選項面板何時打開。我需要一些回調像windowDidBecomeKey;我試圖遵循this question中提供的解決方案,但windowDidBecomeKeywindowDidExpose都不會顯示爲委託方法(但其他人,如windowDidLoad,windowWillLoad等)。如何判斷NSPanel何時獲得焦點或成爲關鍵?

爲了澄清正是我的意思「不出現委託方法」,我的意思是,他們不會在自動完成顯示當我開始鍵入的方法名。我確實嘗試定義它們,但它們從未被調用過。

請問NSPanel對象缺少這些方法,還是有什麼我需要做的?

目前,我有一個接口PrefWindowController

PrefWindowController.h:

#import <Cocoa/Cocoa.h> 

@interface PrefWindowController : NSWindowController 
    //Delegate methods like windowDidBecomeKey appear to not be available here 
@end 

PrefWindowController.m:

@implementation PrefWindowController 

- (id)initWithWindow:(NSWindow *)window 
{ 
    self = [super initWithWindow:window]; 
    if (self) { 
     NSAlert *alert = [[[NSAlert alloc] init] autorelease]; 
     [alert setMessageText:@".."]; 
     [alert runModal]; 
    } 

    return self; 
} 

- (void)windowDidLoad 
{ 
    NSAlert *alert = [[[NSAlert alloc] init] autorelease]; 
    [alert setMessageText:@"Loaded"]; 
    [alert runModal]; 
} 

@end 

當從的.xib在應用程序啓動,windowDidLoad火災和通知的窗口加載顯示了上面定義的。我這樣做只是爲了測試方法實際上被調用。

任何有關如何在面板變爲關鍵點或焦點時獲得回調的建議將非常有幫助。

更新:

我添加了一個windowDidBecomeKey方法窗口控制器,例如:

PrefWindowController.h:

- (void)windowDidBecomeKey:(NSNotification *)notification; 

PrefWindowController.m: - (無效)windowDidBecomeKey:(NSNotification *)通知NSLog(@「Test」); }

測試消息得到記錄我第一次打開窗口,但在我main.m文件中的返回行我得到的錯誤:`線程1:計劃接收的信號:「EXC_BAD_ACCESS」

回答

8

NSWindowDelegate協議有以下方法

- (void)windowDidBecomeKey:(NSNotification *)notification 
- (void)windowDidResignKey:(NSNotification *)notification 

,所以你可以設置你的NSWindowController爲NSWindow代表獲得此回調。您也可以註冊這些通知:

NSWindowDidResignKeyNotification 
NSWindowDidBecomeKeyNotification 

NSPanel是NSWindow子類,所以所有這些行爲適用於您的情況。

+0

'windowDidBecomeKey'方法似乎工作 - 但看到我的更新問題。這可能與我沒有使用窗口管理器打開窗口(並且窗口在關閉時被設置爲釋放)有關嗎?不過,我有點不清楚我會如何從另一個班級那樣做。也就是說,獲取一個PrefWindowManager的實例,我可以用它打開窗口。 – WilHall

+0

你有日誌嗎?沒有更多的信息很難說。 – jbat100

+0

我解決了這個問題 - 這實際上是因爲窗口關閉了。謝謝! – WilHall

相關問題