Domanda

I need to update the value of a NSProgressIndicator through a class different from the one where the outlet was defined.

Es:

  • In class A.h I define the outlet for the NSProgressIndicator and link it.
  • In class A.m I have a method to update the doubleValue of the Indicator.
  • In class B.m I call, through an istance of class A, the method but nothing happens.

Istead if i call the same method within class A it works fine.

What am I missing ?

//REQUESTED CODE:
//Class A (AppDelegate)
A.m
//Other stuff
-(void)update_indicator:(double)value {
//Method that updates the value of the indicator
[progress_indicator setDoubleValue:(double)value];
[progress_indicator setNeedsDisplay:YES];
}

//Class B (Drawing class - subclass of NSView)
B.m
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
//Create istance;
A *istance = [[A alloc] init]; //Alloc class A and init it
[A update_indicator:50.0];
//Other code..
}

The strange thing is that if I call -(void)update_indicator:(double)value within class A, where the outlet is defined, it works fine, if I call it from B it is being called but the progress bar is not showing.

È stato utile?

Soluzione

If A is the delegate class of the application you can get the good instance using this [[NSApplication sharedApplication]delegate], instead of creating a new instance.

Otherwise, you should make class A a singleton.

Take a lock at this example http://www.galloway.me.uk/tutorials/singleton-classes/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top