How can I conditionally declare a delegate in an interface declaration?

I have an Xcode 4 project that builds up to two different goals. I defined some constants in the build settings so that I could run different codes for each goal, for example:

#ifdef VERSION1 // do this #else // do that #endif 

In one version of the application, I need a main view controller to open another view controller and become its delegate, but another version does not use this view controller and should not compile its code or try to become its delegate. I set the title of the main view controller as follows:

 #ifdef VERSION2 #import "SpecialViewController.h" #endif @interface MainViewController : UIViewController <MPMediaPickerControllerDelegate, SpecialViewControllerDelegate> { // etc. 

The condition associated with the #import tag works fine, but how can I declare this class as SpecialViewControllerDelegate in one version, but not in another?

+6
source share
2 answers

Just use the #define preprocessor directive to change delegates between versions. Here is an example for "VERSION2".

 #ifdef VERSION2 #import "SpecialViewController.h" #define ARGS PMediaPickerControllerDelegate, SpecialViewControllerDelegate #endif @interface MainViewController : UIViewController <ARGS> 
+10
source

Until you appoint a delegate, you must stop implementing. Your SpecialViewController in VERSION1 (if you even have a SpecialViewController in V1) will not have a delegate, so its calls will not be anywhere, which will not lead to side effects.

 #ifdef VERSION2 specialViewController.delegate = self; #endif 

If this approach does not work, it seems that you should have a different MainViewController for each purpose.

+1
source

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


All Articles