-1

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++;
}
Community
  • 1
  • 1
modusCell
  • 13,151
  • 9
  • 53
  • 80
  • That line before `windowDidLoad` declares a global variable named `i`. You **must** delete that line, as it doesn’t make sense. Besides: what rdelmar writes… – danyowdee Feb 13 '13 at 07:56

2 Answers2

0

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

@private
    int i;
modusCell
  • 13,151
  • 9
  • 53
  • 80
-1

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).

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • ok, when a window initialised a timer starts counting 0 to ~ and show value on a textfield. when i create second window from same nib timer starts working but not start from 0, it starts where first timer is. – modusCell Feb 12 '13 at 19:50
  • @mohacs, how are you sharing this timer? Where do you create it? – rdelmar Feb 12 '13 at 19:52
  • i am trying to avoid sharing. i have declared as a property and once i saw this case i declared locally but still. i hope i can tell my problem. in first window it is increasing i++ with timer when i open second window i comes with the existing value at window1. – modusCell Feb 12 '13 at 19:59
  • @mohacs, I'm not sure I understand your setup. You have an IBAction in the app delegate that creates an new window controller and shows its window, correct? Are you saying that if you click that twice, the second window's timer starts where the first one's timer is currently? – rdelmar Feb 12 '13 at 20:49
  • yes exactly what i describe. if first window counter value is 10 when i click second time second window appear and second window counter starts from 11. – modusCell Feb 12 '13 at 20:55
  • @mohacs, I've edited my answer. I think it should fix your problem. – rdelmar Feb 12 '13 at 21:26
  • have tried that when i set i = 0 in windowDidLoad this time when second window open it reset "i" in first window. all of these non sense to me i alloc new object how can both use same variable can't understand. by the many thanks working on this. – modusCell Feb 12 '13 at 21:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24403/discussion-between-rdelmar-and-mohacs) – rdelmar Feb 12 '13 at 22:34