Objective-c – How to implement the Method makeKeyAndOrderFront:

cocoaobjective c

I am making a new window open and would like to implement the method makeKeyAndOrderFront: for the window, i was wondering what code i would need to enter to do this.

Here is some of the code I've already got to open the window:

File 1 (The First Controller)

#import "PreferenceController.h"


@implementation PreferenceController

- (id)init
{
    if (![super initWithWindowNibName:@"Preferences"])
        return nil;
    return self;
}
- (void)windowDidLoad
{
    NSLog(@"Nib file is loaded");
}

File 2 (The Action Opening The Window)

#import "Prefernces_Delegate.h"
#import "PreferenceController.h"

@implementation Prefernces_Delegate

- (IBAction)showPreferencePanel:(id)sender
{
    // Is preferenceController nil?
    if (!preferenceController) {
        preferenceController = [[PreferenceController alloc] init];
    }
    NSLog(@"showing %@", preferenceController);
    [preferenceController showWindow:self];
}

The reason I am trying to do this is it has been suggested by a friend to solve a window opening problem.

Best Answer

Somewhere after [preferenceController showWindow:self];:

[self.window makeKeyAndOrderFront:self];

or did you mean add a method to the controller?

// you should use a different method name, cause it's not the
// controller that is made key and ordered front.
- (void)makeKeyAndOrderFront:(id)IBAction {
    [self.window makeKeyAndOrderFront:self];
}
Related Topic