objlv
objlv

Reputation: 601

Dealing with views and recursion

Here is my code

classA.h

#import classB.h

@interface classA : UIViewController
{
  classB *classb;
}

@property (nonatimic, retain) classB *classb;
@end

classA.m

@implementation classA
@synthesize classb = _classb;

-(void)someMethod
{
  self.classb = [[classb alloc]initWithNibName:@"classb" bundle:nil];
  [self.view.superview addSubview:self.classb.view]; 
  [self.view removeFromSuperview];

}
@end

That code work well. classb view is loaded normally. The problem starts here

classB.h

#import classA.h

    @interface classB: UIViewController
    {
       classA *classa;
    }
    @property (nonatimic, retain) classA *classa;
    @end

Now in classA I get error

Unknown type name classB;

I think the problem is some kind of recursion. The idea is classA to load classB view and in some point classB to remove self from superview and add classA as view

Upvotes: 1

Views: 57

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

move the #import classA.h from classB.h to classB.m and add @class classA to the header file of classB

If you use a class only in an @interface (i.e. you don't use a @protocol defined in that class header) it's enough to use the @class SomeClass statement.

This will prevent circular includes.


classB.h:

@class classA;

@interface classB: UIViewController
{
   classA *classa;
}
@property (nonatomic, retain) classA *classa;

@end

classB.m:

#import "classB.h";
#import "classA.h";

@implementation classB 
/* */
@end

Upvotes: 2

Related Questions