Joseph Girolamo
Joseph Girolamo

Reputation: 3

NSWindow won't display in OSStatus event handler method

I've been stuck on this for quite a while and I've searched the Internet far and wide for a solution... The connections in IB are setup correctly and I can open up my NSWindow in awakeFromNib like so:

NSApp activateIgnoringOtherApps:YES];
[popUp makeKeyAndOrderFront:nil]; 

with no problems. I can also call a method from awakeFromNib and open the window using those same two lines in the method.

The problem arises in this method and in any methods called from it - The NSWindow will not open:

OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData)
{ 
    GeronimoAppDelegate *self = [[GeronimoAppDelegate alloc] init];
    [self.popUp makeKeyAndOrderFront:nil];

    GeronimoAppDelegate *appDel = (GeronimoAppDelegate *)[NSApp delegate];
    [appDel.popUp makeKeyAndOrderFront:nil];

    //Run the timer method
    [self runBackTimer];
    return noErr; 
}

As you can see, I've tried two approaches to opening the window by referring to the appdelegate to access the NSWindow object, but neither have worked. I need to be able to open the window from this method or a method called from it. I might be referring to the object incorrectly? (All of these methods are in the same file - GeronimoAppDelegate.m)

Upvotes: 0

Views: 198

Answers (2)

Ken Thomases
Ken Thomases

Reputation: 90641

The hot key handle function, being a C function rather than an Objective-C instance method, doesn't have a notion of a "self" object. You've tried to work around this by declaring a self local variable, but it doesn't point to any object of interest. Instead, you've created a new and different object for it to point to, one that isn't connected to the rest of the objects in your app and doesn't have its popUp property connected to anything (because it's not coming from a NIB).

Referring to the app delegate should have worked. Are you sure the delegate outlet of the application object is connected to the app delegate object which is instantiated in the NIB? Could you be later disconnecting it or reconnecting it by calling -setDelegate: or assigning to its .delegate property?

Typically, to integrate a C-style callback with an object-oriented program, you need to arrange to pass an object pointer into the C function. Luckily, the hot key callback signature accommodates such a design with the userData parameter. When you register the callback, you should specify the object pointer as the user data that should be passed into the callback when it's called. In the callback, you declare an object pointer variable of the appropriate type and assign userData to it (under ARC this requires a __bridge cast).

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299545

The fact that it opens in awakeFromNib but not elsewhere suggests that it's not actually wired, but the window is marked "open at startup" so it just happens to work. Make sure that popUp is not actually nil.

Upvotes: 0

Related Questions