关闭它并设置Focus后再次显示窗口

时间:2011-11-13 13:18:21

标签: objective-c macos cocoa window

我有两个简短的问题:

  1. 关闭窗口后如何重新打开窗口。
  2. 如何将焦点设置在我打开的窗口上。
  3. 如果有人可以帮助我会很好!

    这是我到目前为止的源代码,但我不知道如何继续:

    调用WindowController的方法:

    - (IBAction)openPreferences:(id)sender 
    {
    [NSApp activateIgnoringOtherApps:YES];
    
    if (NULL == preferences) 
    {
        preferences = [[PreferencesController alloc] initWithWindowNibName:@"Preferences"];
    }
    [preferences showPreferenceWindow];
    }
    

    这是PreferencesController的标头:

    #import <Foundation/Foundation.h>
    
    @interface PreferencesController : NSWindowController <NSWindowDelegate>
    - (void)showPreferenceWindow;
    @end
    

    这是PreferencesController的主要内容:

     #import "PreferencesController"
    
     @interface PreferencesController()
     @end
    
     @implementation PreferencesController
    
     - (void)windowWillClose:(NSNotification *)notification
     {
    
     }
    
     // display the preference window
     - (void)showPreferenceWindow
     {
         [self.window makeKeyAndOrderFront:NSApp];
    
         // TODO: window should be focused and if the user press the close button it should be displayed again
     }
    
     - (void) dealloc
     {
        [super dealloc];
     }
    
     @end
    

1 个答案:

答案 0 :(得分:3)

NSWindow有这个方便的方法- (void)setReleasedWhenClosed:(BOOL)releasedWhenClosed。将其设置为NO,您的窗口可以无限次打开。关于你的焦点问题:它应该已经在努力使你的窗口成为焦点。

但是因为你正在使用偏好窗口:我可以建议DBPrefsWindowController它已经很老了,但它今天仍然有效。

相关问题