0

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
Eladar
  • 151
  • 1
  • 8

1 Answers1

3

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
aapierce
  • 897
  • 9
  • 14
  • Before I try to implement your code, can you explain me why u use *_secondViewController; instead *secondViewController; Sorry I am new on iOS. – Eladar Mar 13 '14 at 18:17
  • 1
    The underscore prefix is just a common instance variable naming convention in Objective-C. – Aaron Mar 13 '14 at 18:21
  • @aapierce Ok, now i seen that your code works, but the problem now is. The ViewControllers overlap. How i can do for solvent this? – Eladar Mar 13 '14 at 18:44
  • @Eladar That's a completely separate issue altogether. I recommend that you submit a new Stack Overflow question about that. However, your issue is probably either because you didn't tell your old view to `removeFromSuperview`, or maybe you need to tweak the `frame` of one (or both) of your views. – aapierce Mar 13 '14 at 19:41