PDA

View Full Version : applicationWillTerminate not working?




Darkroom
Jun 22, 2008, 09:04 PM
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?


- (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];
}
}



Krevnik
Jun 22, 2008, 10:31 PM
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?


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.

lancestraz
Jun 22, 2008, 10:54 PM
Use "applicationShouldTerminate" instead. That way you can return YES or NO depending on mainWindow's current alphaValue.

- (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];
}
}

Darkroom
Jun 22, 2008, 11:21 PM
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.

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.

Use "applicationShouldTerminate" instead. That way you can return YES or NO depending on mainWindow's current alphaValue.

worked perfectly! thanks so much!

tskoti
Feb 9, 2009, 03:50 PM
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

gnasher729
Feb 9, 2009, 05:10 PM
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

developer.apple.com

Oh, sorry, for you it should be

dvlpr.ppl.cm