【问题标题】:Display window again after closing it and setting Focus关闭窗口并设置焦点后再次显示窗口
【发布时间】:2011-11-13 13:18:20
【问题描述】:

我有两个简短的问题:

  1. 如何在关闭窗口后重新打开它。
  2. 如何将焦点设置在我打开的窗口上。

如果有人可以帮助我,那就太好了!

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

调用WindowController的方法:

- (IBAction)openPreferences:(id)sender 
{
[NSApp activateIgnoringOtherApps:YES];

if (NULL == preferences) 
{
    preferences = [[PreferencesController alloc] initWithWindowNibName:@"Preferences"];
}
[preferences showPreferenceWindow];
}

这是 PreferencesController 的 Header:

#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

【问题讨论】:

    标签: objective-c macos cocoa window


    【解决方案1】:

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

    但是由于您使用的是首选项窗口:我可以建议DBPrefsWindowController 它已经很老了,但它今天仍然有效。

    【讨论】:

    • 感谢您的回答!您发送给我的链接非常有帮助:) 我在创建实例后调用了方法 deletedWhenClosed。但是没有任何新的事情发生。仍然只能显示一次窗口。在我看来,该方法不像我使用它那样有效。首选项 = [[IEMacPreferences alloc] initWithWindowNibName:@"Preferences"]; [[首选项窗口] setReleasedWhenClosed:NO];
    • 好的解决了这个问题。在 Interface Builder 中出现了另一个错误。做了一些愚蠢的事情(创建了我自己的文件所有者并且没有使用默认的)请不要问我为什么;)
    • DBPrefsWindowController 获胜!
    猜你喜欢
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2019-01-29
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多