Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
Hi,

I have an application where I want to implrement the following:
When user quits the application, it must show a confirmation dialog and react accordingly.
I want to know where to catch the quit signal and implement an alert dialog and proceed further.
Looking for some help.

Regards,
Satyam.:)
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
First rule.

When posting a question like this, platform, language etc are tiny little bits of information that become useful when formulating a response.

I'm going to assume Cocoa and Objective-C.

You need to create a delegate that is attached to the NSApplication that will wait for applicationClosing or windowClosing event. In your code you can display the dialog you created in IB or an programatically created panel and then return YES or NO depending on the response.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
I'm not sure of the reason for your alert, but be aware that if you're making a document-based Cocoa application, and you're keeping track of changes, Cocoa will automatically take care of popping up alerts when the user tries to quit with open, unsaved documents.
 

maxrobertson

macrumors 6502a
Jun 15, 2006
581
0
Jakarta
I know this might not be what you're talking about, but do not under any circumstances make an app that asks "Are you sure you want to quit?" when the user selects quit from the menu and doesn't have a document or anything unsaved. It's unbelievably annoying.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
I know this might not be what you're talking about, but do not under any circumstances make an app that asks "Are you sure you want to quit?" when the user selects quit from the menu and doesn't have a document or anything unsaved. It's unbelievably annoying.
Maybe he's writing the app that prevents nuclear missiles from firing? :D
 

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
I am using Cocoa with Obj C.
I created a delegate and added the following code in .m file as well as decleration in .h file
Still it is not showing the alert message. Please help me.
I am using the following code.

- (BOOL)windowShouldClose:(NSWindow *)sender
{
int answer = NSRunAlertPanel(@"Close",@"Are you Certain?",
@"Close",@"Cancel", nil);

switch(answer)
{
case NSAlertDefaultReturn:
return YES;
default:
return NO;
}
}
 

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
i have gone through website "Graceful Application Termination." but it didn't mention what are the events that are generated for handling "application quit".

i want to ask user whether to quit or not using "NSRunAlertPanel". depending on user's choice I want to quit or not.

windowClosing and applicationClosing are available with java.
 

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
I got the solution.
Here is the code :

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
int answer = NSRunAlertPanel(@"Close",@"Are you Certain?",
@"Close",@"Cancel", nil);

if(answer == NSAlertDefaultReturn)
return NSTerminateNow;

return NSTerminateCancel;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.