Quitting from application

Discussion in 'Mac Programming' started by satyam90, Oct 19, 2007.

  1. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #1
    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.:)
     
  2. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #2
    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.
     
  3. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    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.
     
  4. maxrobertson macrumors 6502a

    Joined:
    Jun 15, 2006
    Location:
    Jakarta
    #4
    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.
     
  5. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #5
    Maybe he's writing the app that prevents nuclear missiles from firing? :D
     
  6. satyam90 thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #6
    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;
    }
    }
     
  7. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    From the documentation:

    Also see Graceful Application Termination.
     
  8. satyam90 thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #8
    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.
     
  9. satyam90 thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #9
    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;
    }
     

Share This Page