Eladar
Eladar

Reputation: 151

I cant use #import "Header.h" in two different files

I have 3 ViewControllers. I want them to access each other, i mean.

VC1 have to acces VC2 and VC3.

VC2 have to acces VC1 and VC3.

VC3 have to acces VC1 and VC2.

When I want to change to another ViewController, I do the follow:

I import the header of the ViewController that I can to change:

For example, in the VC1:

#import "VC2"
#import "VC3"

and then I do:

VC2 *myVC2;
VC3 *myVC3;

then I change the view controller with:

myVC2 = [[VC2 alloc] initWithNibName:@"VC2" bundle: nil];
myVC-C = [[VC3 alloc] initWithNibName:@"VC3" bundle: nil];

[self.view addSubView:myVC2.view];

or

[self.view addSubView:myVC3.view];

But, this method is usable when u have 2 ViewController. Now in the third ViewController I try to import the headers and declare the:

VC3 *myVC3;

VC1 *myVC1;

And the compiler says me an Fix.

He suggested me to change the name of the class, as if you were doing something wrong.

What is the method to call more than one ViewControllers? I do not want to use TabBars. I use 3 buttons that I copy in each view to access between the ViewControllers.

Regards.

Upvotes: 0

Views: 100

Answers (1)

aapierce
aapierce

Reputation: 897

Based on your description of the problem, my guess would be that your issue is regarding "circular dependancies." In general, it's best to avoid using #import in your header (.h) files. Instead, use @class to indicate that the class exists, and place your #import directives in the implementation (.m) files to actually use the class.

Example:

MyViewController.h

#import <UIKit/UIKit.h>

@class MySecondViewController;

@interface MyViewController : UIViewController {
     MySecondViewController *_secondViewController;
     // ...
}

// ...

@end

MyViewController.m

#import "MyViewController.h"
#import "MySecondViewController.h"

@implementation MyViewController

// ...

@end

Upvotes: 3

Related Questions