Quit Error (Have To Quite Twice To Terminate)

Discussion in 'Mac Programming' started by Darkroom, Nov 5, 2007.

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    hi there...

    i'm having a problem with my quit function... my app will only quit if i quite twice – that is, i have to press Apple+Q two times for the app to terminate.

    it may or may not have to do with my timer... i'm not sure... but something very strange is happening to the object as the "quitApplication" action will be added twice, no matter how many times i rewrite and set up the object. i've included an image to show you what i mean:

    [​IMG]

    here's the code for the Fade Controller:

    FadeController.h
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface FadeController : NSObject {
        IBOutlet NSWindow *mainWindow;
    	NSTimer *timer;
    }
    
    - (IBAction)quitApplication:(id)sender;
    
    @end
    
    FadeController.m
    Code:
    #import "FadeController.h"
    
    @implementation FadeController
    
    - (void)awakeFromNib {
        [mainWindow setDelegate:self];
    	[mainWindow setAlphaValue:0.01];
    	timer = [[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(fadeIN:) userInfo:nil repeats:YES] retain];
    }
    
    - (void)fadeIN:(NSTimer *)theTimer {
    	if ([mainWindow alphaValue] > 0.0) {
    	[mainWindow setAlphaValue:[mainWindow alphaValue] + 0.1];
    	}
    	else
    	{
    	[timer invalidate];
        [timer release];
        timer = nil;
    	}
    }
    
    - (IBAction)quitApplication:(id)sender {
    	[mainWindow setAlphaValue:1.0];
    	timer = [[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(fadeOUT:) userInfo:nil repeats:YES] retain];
    }
    
    - (void)fadeOUT:(NSTimer *)theTimer
    	{
    	if ([mainWindow alphaValue] > 0.0) {
    	[mainWindow setAlphaValue:[mainWindow alphaValue] - 0.1];
    	}
    	else
    	{
    	[timer invalidate];
        [timer release];
        timer = nil;
    	[NSApp terminate:nil];
    	}
    }
    
    
    @end
    
     
  2. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    ok so i fixed it... works fine now... it was a problem with my timer loop and not a bug with xc/ib3... i changed apart of the code to this to make the loop work:

    Code:
    - (void)awakeFromNib {
        [mainWindow setDelegate:self];
    	[mainWindow setAlphaValue:0.0];
    	timer = [[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(fadeIN:) userInfo:nil repeats:YES] retain];
    }
    
    - (void)fadeIN:(NSTimer *)theTimer
    	{
    	if ([mainWindow alphaValue] < 1.0) {
    	[mainWindow setAlphaValue:[mainWindow alphaValue] + 0.1];
    	}
    	else
    	{
    	[timer invalidate];
        [timer release];
        timer = nil;
    	}
    }
    
    however, it still strange why my Fade Controller is showing 2 quitApplication actions... if i try to remove one from the list by clicking on the "x", they both get disconnected... perhaps this is a bug in the software?
     
  3. macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #3
    Don't know about the twin quit actions, but it looks like you might have some interesting results there of the "dueling timers" variety if somebody were to try quitting the application while it's still fading in. Maybe you want to check for and kill any existing timer before setting a new one in the quit action.

    There seem to be some other more minor issues associated with quitting while a fade is in progress (in or out). Might want to temporarily set your timer interval higher so you can experiment with the scenario more easily and fix what breaks.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Did you try a simple relaunching of IB?
     
  5. macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #5
    How did u associate - (IBAction)quitApplication:(id)sender; to menu item? I mean from where you are calling the above method.
     

Share This Page