开发者

Objective-C NSWindow remove window from key

开发者 https://www.devze.com 2023-02-08 10:44 出处:网络
Hi need a little help, i have a window which is set to always show in the top right corner but it is not set to always 开发者_运维百科key. The window has a few buttons on it and when a button is click

Hi need a little help, i have a window which is set to always show in the top right corner but it is not set to always 开发者_运维百科key. The window has a few buttons on it and when a button is clicked the window becomes key, but what i want it to do is when a button is clicked i want the window to remove itself from being key.

So ideally the window becomes key when a button is clicked and in the method which the button calls i want to write a statement which will then perform the action of the button and remove the window from key.

However the window is declared under the app delegate and the method linked to the button is declared in a separate header file.

Anyone have any ideas how i can do this, any help would be much appreciated. Thanks in advance, Sami.


There are a few solutions depending on the architecture of your application.

  1. Send [[NSApp mainWindow] makeKeyWindow], which will make the main window become key.

  2. Your application delegate could have a reference to the main window. In the action method that handles the button click, you could ask the application delegate to make the main window become key. The application delegate would send [mainWindow makeKeyWindow].

  3. Your application delegate could have a reference to the window controller that manages the main window. In the action method that handles the button click, you could ask the application delegate to make the main window become key. The application delegate would ask the main window controller to do that, and the main window controller would send [[self window] makeKeyWindow].

  4. Your application delegate could listen to the NSWindowDidResignKeyNotification notification and keep a reference to the last window that resigned being key. In the action method that handles the button click, you could ask the application delegate to return key status to that previous window. The application delegate would need to ignore NSWindowDidResignKeyNotification notifications when the window is your auxiliary window. This solution is better when there’s no single main window.

If the first solution is not applicable, a) your application delegate could conform to a protocol that declares a method responsible for restoring key status to the proper window, or b) your action method could post a notification informing the application that your action method has completed, and have the application delegate listen to that notification and restore key status to the proper window.

Note that even though I’ve suggested that the application delegate would implement the behaviour of restoring key status, other objects could be responsible for that. This is particularly easier when notifications are used since there’s no need to grab a reference to the object that will restore key status due to the inherent loose coupling provided by notifications.

0

精彩评论

暂无评论...
验证码 换一张
取 消