14

Ok, this is really stumping me:

I create a modal sheet on a window as

-(IBAction) login: (id) sender {
[NSApp beginSheet:loginWindow 
   modalForWindow:window 
    modalDelegate:nil 
   didEndSelector:nil 
      contextInfo:nil];
}

and try to remove it with

-(IBAction) loginWindowCancelPressed:   (id) sender {
debugLog(@"cancel");
[NSApp endSheet:loginWindow];
}

but it remains.

I've checked all of the obvious things like IB connections and ensuring that window and loginWindow are present and in the normal state.

So, the question is: Am I missing something?

2 Answers 2

20

You have to set up a delegate for the sheet. The delegate should implement a method with a signature like the following:

- (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;

(You can use a different name if you like, but it should accept the same parameters.)

You can do whatever you need to do in that method, but ultimately it should end with:

[sheet orderOut:self];

which will dismiss the sheet.

4
  • 2
    You don't actually need a delegate (it's optional). The only thing missing is the call to orderOut:
    – Nik
    Jan 8, 2012 at 2:46
  • 1
    I have setup a delegate and selector. The sheet is controlled by a window controller. Calling endSheet: has no effect (the delegate is NEVER notified). Calling stopModal works, but it is wrong and causes problems when displaying further sheets. WTF? Cocoa for the desktop really sucks big time. 90% of the time spent dealing with these idiosyncracies... Apr 16, 2012 at 14:13
  • calling orderOut on an instance of NSWindow closes the window, not the sheet.
    – auco
    Jun 27, 2013 at 14:15
  • @auco: A sheet is an instance of NSWindow.
    – mipadi
    Jun 27, 2013 at 18:00
5

See: Creating a Modal Dialog or Window in Cocoa Objective-c?

A delegate is not required. The following works for me to dismiss and allows calling it back later:

[NSApp endSheet: loginWindow];
[loginWindow orderOut:self];
1
  • For pure simplicity, this is a useful solution. Works with the new storyboard system for OSX as well. Apr 4, 2016 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.