applicationWillTerminate not working?

Discussion in 'Mac Programming' started by Darkroom, Jun 22, 2008.

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i'm attempting to fade my windows on quit, but the following code only seems to work if i have a custom quit method like -(IBAction)quitApp:(id)sender. i'm trying to override the applictionWillTerminate so the quit from the dock menu will also fade out windows, instead of abruptly quit. i know this new applicationWillTerminate method is getting called correctly because NSLog is outputting text before the app quits, but the fade out isn't being called. is it because this function is relying on a secondary funtion to complete it's task? how can i write it all in one function? any thoughts?

    Code:
    - (void)applicationWillTerminate:(NSNotification *)notification
    	{
    	NSLog (@"Application Terminate Override");
    		
    	SetSystemUIMode(kUIModeNormal, 0);
    	timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(allWindowsFadeOUT:) userInfo:nil repeats:YES] retain];
    	}
    	
    - (void)allWindowsFadeOUT:(NSTimer *)theTimer
    	{
    	if (([mainWindow alphaValue] > 0.0) || ([secondWindow alphaValue] > 0.0)){
    	[mainWindow setAlphaValue:[mainWindow alphaValue] - 0.1];
    	[secondWindow setAlphaValue:[secondWindow alphaValue] - 0.1];
    	}
    	else
    	{
    	[timer invalidate];
    	[timer release];
    	timer = nil;
    	[NSApp terminate:nil];
    	}
    }
    
    
     
  2. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #2
    Yeah, the timer gets killed pretty much immediately after you create it.

    Now first, my rant is that you probably shouldn't want to override the close behavior of the window. If someone is quitting your app, they want it gone. If your app lingers too long, then you get into a state where you could cancel restarts and shutdowns (bad).

    That said, the easiest way to do this is via CoreAnimation rather than trying to do it yourself.
     
  3. macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #3
    Use "applicationShouldTerminate" instead. That way you can return YES or NO depending on mainWindow's current alphaValue.

    Code:
    - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
    	timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(allWindowsFadeOUT:) userInfo:nil repeats:YES] retain];
    	
    	if ([mainWindow alphaValue] > 0.0)
    		return NO;
    	else
    		return YES;
    }
    
    
    - (void)allWindowsFadeOUT:(NSTimer *)theTimer
    {
    	if ([mainWindow alphaValue] > 0.0) {
    		[mainWindow setAlphaValue:[mainWindow alphaValue] - 0.1];
    	}
    	else
    	{	
    		[timer invalidate];
    		[timer release];
    		timer = nil;
    		[NSApp terminate:nil];
    	}
    }
     
  4. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    you're not the first to tell me that... and i won't disagree with you, it's probably bad... but the fadeout is really quick, and i think it adds a lot... i haven't studied core animation yet, but i'm looking forward to it in upcoming chapters of my dev book.

    worked perfectly! thanks so much!
     
  5. macrumors newbie

    Joined:
    Nov 5, 2008
    #5
    how to handle Interruptions in Iphone

    Hi All,
    I am new to this Iphone Application Development, Plz help me with a sample code for how to handle the Interruptions (like incoming call, SMS alert, reminders) in Iphone,

    It is very Urgent, any material for that also will be helpful,

    Plz help me,

    Thanks in advance,
    Koti
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    developer.apple.com

    Oh, sorry, for you it should be

    dvlpr.ppl.cm
     

Share This Page