如何在接口声明中有条件地声明委托?

时间:2021-04-06 20:53:28

I have an Xcode 4 project that builds to two different targets. I've defined some constants in the build settings so I can run different code for each target like this:

我有一个Xcode 4项目,它构建了两个不同的目标。我已经在构建设置中定义了一些常量,因此我可以为每个目标运行不同的代码,如下所示:

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

In one version of the app, I need the main view controller to open another view controller and become its delegate, but the other version doesn't use that view controller and shouldn't compile its code or try to become its delegate. I've set up the main view controller header like this:

在应用程序的一个版本中,我需要主视图控制器打开另一个视图控制器并成为其委托,但另一个版本不使用该视图控制器,不应编译其代码或尝试成为其委托。我已经设置了主视图控制器头像这样:

#ifdef VERSION2
#import "SpecialViewController.h"
#endif

@interface MainViewController : UIViewController <MPMediaPickerControllerDelegate, SpecialViewControllerDelegate> {
// etc.

The conditional around the #import tag works fine, but how can I declare this class to be the SpecialViewControllerDelegate in one version but not the other?

围绕#import标签的条件工作正常,但是如何在一个版本中将此类声明为SpecialViewControllerDelegate而不是另一个版本?

2 个解决方案

#1


10  

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

只需使用#define预处理程序指令来更改版本之间的委托。这是“VERSION2”的一个例子。

#ifdef VERSION2
#import "SpecialViewController.h"
#define ARGS PMediaPickerControllerDelegate, SpecialViewControllerDelegate 
#endif

@interface MainViewController : UIViewController <ARGS>

#2


1  

As long as you don't assign the delegate you should be fine leaving the implementation. Your SpecialViewController in VERSION1 (if you even have a SpecialViewController in V1) will not have a delegate so its calls will go nowhere, which should lead to no side effects.

只要你没有分配代表,你应该没有实施。你在VERSION1中的SpecialViewController(如果你在V1中有一个SpecialViewController)将没有委托,所以它的调用无处可去,这应该没有副作用。

#ifdef VERSION2
specialViewController.delegate = self;
#endif

If this approach doesn't work it almost seems like you should have a different MainViewController for each target.

如果这种方法不起作用,几乎看起来你应该为每个目标使用不同的MainViewController。

#1


10  

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

只需使用#define预处理程序指令来更改版本之间的委托。这是“VERSION2”的一个例子。

#ifdef VERSION2
#import "SpecialViewController.h"
#define ARGS PMediaPickerControllerDelegate, SpecialViewControllerDelegate 
#endif

@interface MainViewController : UIViewController <ARGS>

#2


1  

As long as you don't assign the delegate you should be fine leaving the implementation. Your SpecialViewController in VERSION1 (if you even have a SpecialViewController in V1) will not have a delegate so its calls will go nowhere, which should lead to no side effects.

只要你没有分配代表,你应该没有实施。你在VERSION1中的SpecialViewController(如果你在V1中有一个SpecialViewController)将没有委托,所以它的调用无处可去,这应该没有副作用。

#ifdef VERSION2
specialViewController.delegate = self;
#endif

If this approach doesn't work it almost seems like you should have a different MainViewController for each target.

如果这种方法不起作用,几乎看起来你应该为每个目标使用不同的MainViewController。