How to access a variable from an inner class

 //MainClass.m 

 @interface InnerClass : NSObject{

 }
 @end

 @implementation InnerClass

 -(void)run{
      while(isActive){//want to access this variable which defined in MainClass
      //do something
      }
 }

 @end

 @interface MainClass : NSObject{
      BOOL isActive;
 }
 @end

 @implementation MainClass


 @end

I have a MainClass and it has an inner class (InnerClass). I want to access a variable of type boolean (isActive) defined in the MainClass class from the inner class. What I'm trying to do is that the inner class will work in a separate thread and will check the isActive variable on the main class, and if isActive is false, then it will stop starting a new thread. Thanks in advance...

+5
source share
2 answers

Objective-C . isActive MainClass, InnerClass MainClass InnerClass .

+8

Objective-C , , " " .m MainClass factory ( ) MainClass, " " bool*, &isActive .

MainClass.h

 @interface MainClass : NSObject{
      BOOL isActive;
 }
 @end

MainClass.m

 @interface InnerClass : NSObject{
      BOOL* isActive;
 }

 -(id)initWithActive:(BOOL*)isAct){

      if (self = [super init]) {
         isActive = isAct;
      } 
      return self;
 }

 @end

 @implementation InnerClass

 -(void)run{
      while(*isActive){//want to access this variable which defined in MainClass
      //do something
      }
 }

 @end


 @implementation MainClass

      //Can use [self newInnerClass] to create a new instance of the innerclass
     -(id)newInnerClass{
           return [[[InnerClass alloc] initWithActive:&isActive] autorelease];
      }


 @end
+3

All Articles