PDA

View Full Version : Cocoa: Delegates & NSBeginAlertSheet


XxtraLarGe
2004.01.19, 07:43 AM
I'm trying to make a sheet drop down after a sound is played.

NSSound has the delegate method -(void)sound:(NSSound *)aSound didFinishPlaying:(BOOL)aBool. I figured that I could use that to trigger a sheet. Here's the code that I wrote:

- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)aBool {
NSString *msg = @"You have won the game!";
SEL sel = @selector(sheetClosed:returnCode:contextInfo:);

if (aBool) {
NSBeginAlertSheet(@"Game Over!",
@"Play Again",
@"Close",
nil,
[self window],
self,
sel,
NULL,
[self window],
msg,
nil);
}
}

- (void)sheetClosed:(NSWindow *)sheet
returnCode:(int)returnCode
contextInfo:(void *)contextInfo
{
if (returnCode == NSAlertDefaultReturn) {
[self reset];
}
}


I have this code at the bottom of my Custom View class, and not in a separate Delegate class. It compiles without error, but doesn't do anything. Any ideas?

OneSadCookie
2004.01.19, 02:14 PM
I assume you have something like this somewhere:

[mySound setDelegate:myCustomView];

XxtraLarGe
2004.01.19, 10:41 PM
I assume you have something like this somewhere:

[mySound setDelegate:myCustomView];

Nope! It's always something simple, isn't it?

XxtraLarGe
2004.01.19, 11:00 PM
Nope! It's always something simple, isn't it?

Thanks OSC! It works great!

XxtraLarGe
2004.01.20, 11:37 AM
I have two buttons, one labeled "Play Again" and one labeled "Cancel." How would I get the application to quit if the button currently labeled "Cancel" is pressed? I know it would go into my selector method if the return code was NSAlertAlternateReturn.


- (void)sheetClosed:(NSWindow *)sheet
returnCode:(int)returnCode
contextInfo:(void *)contextInfo
{
if (returnCode == NSAlertDefaultReturn) {
[self reset:[self window]];
}
if (returnCode == NSAlertAlternateReturn) {
// code to quite game goes here
}
}


I can't just put [self terminate] because the view doesn't respond to terminate, and I can't use [super terminate] because both NSView & NSApplication are subclasses of NSResponder.

Josh
2004.01.20, 02:06 PM
I believe [NSApp terminate:self]; would work.

OneSadCookie
2004.01.20, 02:11 PM
[NSApp terminate:self]

XxtraLarGe
2004.01.20, 04:45 PM
I believe [NSApp terminate:self]; would work.

Viola, it worked! I didn't think of this, because it seems to me that terminate is an instance method, not a class method.

Thanks for the help jabber. So many simple, stupid things in Cocoa that are easy to overlook.I don't know what I'd do without AppKiDo & iDevGames!

Steven
2004.01.20, 06:08 PM
The key there is that NSApp is an instance - it's a global variable holding a shared NSApplication. It's there purely for convenience, it's about the same as when you do something like [NSRunLoop currentRunLoop] but shorter.

OneSadCookie
2004.01.20, 07:20 PM
There is an [NSApplication sharedApplication] method, which I believe simply returns NSApp...