从状态栏中的菜单选择重新打开我的应用程序窗口

发布于 2024-12-23 09:22:56 字数 259 浏览 1 评论 0原文

当按下红色 X 时,我的窗口消失,但根据设计,我的状态栏项目保留在状态栏中。

当您单击状态栏项目时,它会打开一个菜单。选择之一是重新打开应用程序。它能够在应用程序控制器中调用操作,但是我不确定如何打开应用程序的窗口备份。我读到

[window makeKeyOrderFront:self];

可以完成此操作,但编译器无法识别“窗口”。我是 Objective C/cocoa 的新手,所以我确信我错过了一些明显的东西。

When the red X is pressed my window goes away but, as designed, my status bar item stays in the status bar.

When you click on the status bar item it opens a menu. One of the choices is to reopen the application. It is able to call an action in the app controller, however I'm not sure what to do to open my application's window back up. I read that

[window makeKeyOrderFront:self];

Would accomplish this but "window" isn't recognized by the compiler. I'm new to objective c/cocoa so I'm sure I'm missing something obvious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经大条 2024-12-30 09:22:56

这里 window 是您用来存储应用程序中的 NSWindow* 对象的变量 - 它假设您仍然将它保存在您的一个类中(通常在应用程序中) delegate - 默认的 Xcode 应用程序委托模板甚至为 window 创建一个属性)。但是,所有这些都取决于您的应用程序的类型 - 如果您的应用程序是基于文档的,这应该是自动的(您可以调用 openUntitledDocumentAndDisplay:error: 来创建新文档)。如果不是,那么实际上完全取决于您的代码来管理窗口 - 通常在应用程序委托中。

Here window is the variable you used to store the NSWindow* object from your application - it assumes that you still are still holding it in one of your classes (typically in the app delegate - the default Xcode app delegate template even creates a property for window). However, all this depends on the type of your application -- this should be all automatic if your application is document-based (you can call openUntitledDocumentAndDisplay:error: to create a new document). If it's not then it is really entirely up to your code to manage the window - typically in the app delegate.

几度春秋 2024-12-30 09:22:56

我最近解决了这个问题,这对我有用。所有三件事都在 AppDelegate 中处理:

// 响应重新打开应用程序菜单选择

- (IBAction)showMainWindow:(id)sender
{
    [self applicationShouldHandleReopen:nil hasVisibleWindows:YES];
    [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}

//​​ 如果您希望主窗口在用户关闭后重新出现,则需要此

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [_window setReleasedWhenClosed:NO]; 
}

// 如果您希望主窗口在用户关闭后重新出现,则需要此

- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag{
    [_window setIsVisible:YES];
    return YES;
}

I recently tacked this, and here's what worked for me. All three things are handled in AppDelegate:

// Response to reopen application menu selection

- (IBAction)showMainWindow:(id)sender
{
    [self applicationShouldHandleReopen:nil hasVisibleWindows:YES];
    [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}

// Need this if you want the MainWindow to reappear after the user has closed it

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [_window setReleasedWhenClosed:NO]; 
}

// Need this if you want the MainWindow to reappear after the user has closed it

- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag{
    [_window setIsVisible:YES];
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文