0

As I understand it, the view of a NSViewController class must be loaded before anything can be changed in the view through setters and getters. The problem is, the view in the controller is supposed to display a countdown, so the view controller class has an NSTimer that will change a stringValue every second. But from what I can see, I have to use something like:

[instanceOfViewControllerClass view] in another class so that the view will load and then setStringValue would work.

I asked a similar question before but the answer was what I said above, at the time I didn't know I would have to continuously change the view.

Basically what I'm looking for is something that works like this:

//ViewControllerClass.m
-(void)startCountdown{
//here I make "invocation" using NSInvocation
[invocation setTarget:self];
[invocation setSelector:@selector(updateCountdown)];
timerLabelUpdater = [NSTimer timerWithTimeInterval:1 invocation:invocation repeats:YES];
}

-(void)updateCountdown {
//x would be calculated here by deducting 1 from an original amount of time
[timeLeftLabel setStringValue:@"x"];
}

Everything is working fine, if I put an NSLog in the updateCountdown it logs perfectly, which means it is running, I just can't get the label to change.

Using the answer above I've tried using [self view] and [self loadView] in several places in the code above, but neither of them make a difference.

I hope I've been clear on what I'm looking for this time, any help would be appreciated.

Elbimio
  • 1,041
  • 2
  • 10
  • 25

1 Answers1

0

How are you initializing the view controller? Is it archived in a nib, or are you doing it in code? I'm thinking that you're not actually telling it which nib it's associated with.

If it's part of a nib, you need to set the correct nib name field in the attributes inspector.

If you're instantiating it in code you need to explicitly tell it what's the name of it's nib.

The easiest way is to override init like so:

- (id)init {
    self = [super initWithNibName:@"MyViewController" bundle:nil];
    if (self) {
        // Initialization code here.
    }
    return self;
}
Francis McGrew
  • 7,264
  • 1
  • 33
  • 30
  • Nothing happened, I was using - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self;} Which I guess isn't all that different. – Elbimio Dec 25 '11 at 18:03
  • Are your creating the view controller in code? Or do you have it in a nib somewhere? If you have if in a nib you need to assign a nib name to it. – Francis McGrew Dec 25 '11 at 18:28
  • I just added the NSViewController classes, there's nothing in the nib. – Elbimio Dec 25 '11 at 18:56