I have two short questions:
- How is it possible to reopen a window after closing it.
- How do I set the focus on the window I open.
It would be nice if someone can help me!
Here is the source code I have so far but I don’t know how to go on:
The method calling the WindowController:
- (IBAction)openPreferences:(id)sender
{
[NSApp activateIgnoringOtherApps:YES];
if (NULL == preferences)
{
preferences = [[PreferencesController alloc] initWithWindowNibName:@"Preferences"];
}
[preferences showPreferenceWindow];
}
This is the Header of PreferencesController:
#import <Foundation/Foundation.h>
@interface PreferencesController : NSWindowController <NSWindowDelegate>
- (void)showPreferenceWindow;
@end
This is the Main of 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
NSWindow has this handy method
- (void)setReleasedWhenClosed:(BOOL)releasedWhenClosed. Set that to NO and you window can open unlimited amounts of times. As to your focus issue: It should already be working to bringing your window to focus.But since you are using a preference window: may I suggest DBPrefsWindowController it is pretty old but it still works today.