Pregunta

there is a nib file and i am creating different window instances with different context, all controls works normally except timer and variables triggered by timers looks shared with all windows. this is how i am create window instances.

#import <Cocoa/Cocoa.h>
#import "MYWindow.h"
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
}
@property (strong) MYWindow *pickerWindow;

--

#import "AppDelegate.h"
@implementation AppDelegate
-(IBAction)newWindow:(id)sender
{
    myWindow = [[MYWindow alloc] initWithWindowNibName:@"MYWindowNIB"];
    [myWindow showWindow:self];
}

Also i am having problem with ARC, when i open new instance of a window previous one releases immediately even i declare it is strong in property that is why compiling AppDelegate with a flag -fno-objc-arc. otherwise as i said what ever i did windows releases immediately. XCode 4.6

edit

int i = 0;
-(void)windowDidLoad
{
    timerMoveOutNavBar = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUP) userInfo:nil repeats:YES];
}

-(void)countUP
{
    [text setStringValue:[NSString stringWithFormat:@"%d", i]];
    i++;
}
¿Fue útil?

Solución

i found the solution, you have to declare variables and all other objects as private.

@private
    int i;

Otros consejos

What do you mean by "share same variables"? You can have a superclass that all the window controllers inherit from, and they will all have the properties and methods you create in the superclass, if that's what you're talking about.

As far as the windows being released, do you have the "Release When Closed" box checked in IB? If so, uncheck that box.

After Edit

The problem has to do with the way you initialize the int variable "i". By putting it outside a method, you're declaring it as a global variable that all instance will see. You should create an ivar and set its value to zero where you create the timer:

@implementation MyWindowController {
   IBOutlet NSTextField *text;
    int i;
}


- (void)windowDidLoad {
    [super windowDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUP:) userInfo:nil repeats:YES];
    i = 0;
}

-(void)countUP:(NSTimer *) timer {
    [text setStringValue:[NSString stringWithFormat:@"%d", i]];
    i++;
    if (i== 50) [timer invalidate];
}

Notice that I added a colon to the selector name -- with a timer, the timer passes itself as the argument to its selector, so you can reference it like I do to invalidate it. But it's ok to do it the way you did by assigning the timer to an ivar or property (timerMoveOutNavBar in your case).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top