Xcode classes cannot "see" each other

I was working on an Xcode project, and everything was great until my two classes stopped recognizing each other. Here is an excerpt:

#import "JBXViewController.h" @interface ViewController2 : UIViewController { JBXViewController *jbx; } 

For some reason, I get the error “Unknown name like“ JBXViewController ”, did you mean“ UIViewController ”? I don’t understand how this is possible, since I import another class just a few lines above. Any ideas would be highly appreciated!

+6
source share
2 answers

When you say that they "cannot see each other," I assume that you mean that you are also importing ViewController2.h into your JBXViewController.h. So you have one header that imports another header that imports the first header, which imports the second, which imports the first again ...

Instead, use a direct link to the JBXViewController:

 @class JBXViewController; @interface ViewController2 : UIViewController { JBXViewController *jbx; } 

And then #import "JBXViewController.h" in your implementation instead (in your ViewController2.m)

+11
source

Firoze Lafeer's answer is correct, but this problem is probably a symptom of the poor design of your code.

I assume that in your application, the JBXViewController is the parent view controller, and it sometimes shows ViewController2 for some specific function. (For example, JBXViewController shows a list of records, and ViewController2 edits one of the records.) In this situation, ViewController2 does not need to know the details of the JBXViewController. Instead, the JBXViewController should provide ViewController2 with the data it needs using the ViewController2 properties, and if ViewController2 should call methods in JBXViewController, they should be part of the delegate protocol.

For example, suppose the JBXViewController currently has the following property, and ViewController2 accesses it:

 @property (strong) JBXObject * currentObject; 

Instead, you should have the currentObject property on ViewController2, and the JBXViewController should set it before displaying the view controller:

 self.myViewController2.currentObject = self.currentObject; [self.navigationController pushViewController:self.myViewController2 animated:YES]; 

This works for one-way communication - the JBXViewController can provide ViewController2 data. If data should be returned to the JBXViewController (other than changing the currentObject properties), you must configure a delegate for ViewController2. For instance:

 @protocol ViewController2Delegate; // forward declaration @interface ViewController2 : UIViewController @property (weak) id <ViewController2Delegate> delegate; ... @end @protocol ViewController2Delegate <NSObject> - (void)viewController2ShouldSave:(ViewController2*)viewController2; - (BOOL)viewController2:(ViewController2*)viewController2 shouldAddSomoflange:(JBXSomoflange*)aSomoflange; @end 

Then the JBXViewController conforms to the protocol:

 @interface JBXViewController : UIViewController <ViewController2Delegate> 

Install the delegate in either Interface Builder or code as follows:

 self.myViewController2.delegate = self; self.myViewController2.currentObject = self.currentObject; [self.navigationController pushViewController:self.myViewController2 animated:YES]; 

And we implement all the methods listed in ViewController2Delegate.

Together, these changes mean three things:

  • ViewController2 does not need specific knowledge of how the JBXViewController works. This means that you no longer need to import JBXViewController.h into ViewController2.h, which solves your immediate problem.

  • JBXViewController is now more flexible. As long as it sets the appropriate properties in ViewController2 and implements any necessary delegation methods, you can change whatever you want in JBXViewController, and ViewController2 will never know or take care of this.

  • ViewController2 is now more flexible. You can use it from other parts of the application or transfer it to another application. You can insert a screen between the JBXViewController and ViewController2.

These changes are not necessary for the application to work on your device and function as you plan. But you will have an easier time on the road if you start using such projects.

+5
source

Source: https://habr.com/ru/post/904582/


All Articles