handle alert message on close of application?

Discussion in 'Mac Programming' started by mkristain, Aug 27, 2011.

  1. mkristain macrumors regular

    Joined:
    Aug 18, 2011
    #1
    hi i want to display a alert message when clicked close button of application and if press 'yes' on message application is exit but is 'no' on message application not close.

    i am doing this but it not works fine on 'no'

    Code:
    
    - (void)windowWillClose:(NSNotification *)aNotification
    {
    	NSAlert *alert = [NSAlert alertWithMessageText:@"\nDo you really want exit?"
    									 defaultButton:@"Yes" alternateButton:NO otherButton:@"No"
    						 informativeTextWithFormat:@""];
    	
    	int result = [alert runModal];
    	
    	if(result == NSOKButton)
    	{
    		
    		[NSApp terminate:self];
    	}
    	else
    	{
    		[[NSNotificationCenter defaultCenter] removeObserver:self];
    		//return NSTerminateCancel;
    	}
    
    }
    
    
    thanks.
     
  2. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #2
    I think you should probably be hooking into applicationWillTerminate instead.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    If you want this to happen when the window is about to close use windowShouldClose:
     
  4. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011

Share This Page