PDA

View Full Version : Simulate window close button click




titaniumdecoy
Dec 19, 2007, 02:03 AM
I thought this was the way to simulate a click on the red close button at the top left corner of a window:

[NSApp sendAction:@selector(performClose:) to:nil from:self];

Oddly, this does not close the window when there is an open unsaved document. (A save dialog appears, and after it is dismissed the window is still open.) Clicking the window's close button does close the window, however.

EDIT: Calling the window's -close method does the trick. Sorry to waste anyone's time.



Sijmen
Dec 19, 2007, 03:02 AM
First off, you're sending a message to nil - that won't do anything.

Secondly, as you said, -[NSWindow close] will close the window. If you want to really simulate clicking the close button (ie, with the highlight of the red button etc), use -[NSWindow performClose].

Nutter
Dec 19, 2007, 04:29 AM
First off, you're sending a message to nil - that won't do anything.

No, he's sending a nil-targeted action message, which is delivered to the first responder (http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/chapter_2_section_6.html).

Sijmen
Dec 19, 2007, 04:54 AM
No, he's sending a nil-targeted action message, which is delivered to the first responder (http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/chapter_2_section_6.html).

Whoops, totally forgot about that. Thanks.