iphone - 如何在编译时使用不同的接口(interface)声明而不混淆 Interface Builder

标签 iphone objective-c cocoa-touch ios interface-builder

如果我的应用程序有多个构建版本,例如付费版本和免费版本,并且我希望根据构建版本拥有不同的界面,这似乎会使 Interface Builder 感到困惑。

例如:

// MyViewController.h
#ifdef FREE
@interface MyViewController : NSObject <UIActionSheetDelegate, ADBannerViewDelegate>
#else
@interface MyViewController : NSObject <UIActionSheetDelegate>
#endif
{
    IBOutlet UILabel* myLabel;
}
- (IBAction) myAction:(id)sender;

当我在 IB 中加载 MyViewController.xib 时,它会在信息窗口中显示警告,例如“‘文件所有者’的‘myLabel’导出已连接到‘我的 View ’,但‘myLabel’不再在 MyViewController 上定义。 "

有没有办法做到这一点以免混​​淆 IB?

我想另一种选择是不#ifdef 它,并且对所有构建都有一个定义。如果我使用符合 ADBannerViewDelegate(需要 iOS 4)的 Controller 进行构建,并在 iOS 3.2 上进行部署,会发生什么情况?如果可行,也许没有问题...

最佳答案

我一直没能找到让 IB 了解您正在使用的结构的方法(我尝试使用我过去制作的付费/免费应用程序来做到这一点)。这就是我解决问题的方式:

我没有在我的@interface 定义中使用条件编译。我只是将类定义为普通类,就好像它是付费的非广告版本一样。我们称它为 MyPaidViewController。

@interface MyPaidViewController : UIViewController <UIActionSheetDelegate> {

然后我创建了一个 MyPaidViewController 的子类,它是免费版本。像这样的东西:

@interface MyFreeViewController : MyPaidViewController <ADBannerViewDelegate> {

由于我的免费版本通常需要使用与付费版本不同的 xib(因为我必须四处移动东西以便为广告腾出空间),所以我会让付费 xib 使用 MyPaidViewController 作为文件所有者和我的免费 xib 使用 MyFreeViewController 作为文件所有者。

Interface Builder 也会看到来自父类(super class)的 IBOutlets,因此您将能够在您的免费 xib 中引用 myLabel。

确保 MyPaidViewController 是付费和免费构建目标的一部分,但 MyFreeViewController 应该只是免费构建目标的一部分。

关于iphone - 如何在编译时使用不同的接口(interface)声明而不混淆 Interface Builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5177040/

相关文章:

iphone - GL_CLAMP_TO_EDGE 应该用在 NPOT 纹理中

ios - 关闭带有 UITableView 的模态 UIViewController 时的问题

ios - AFNetworking 2.0 POST 问题 |替换已弃用的 multipartFormRequestWithMethod :path:parameters

ios - 使用 CoreData 在两个队列之间传递对象引用的最佳实践是什么?

iphone - 在 iPhone 核心数据中的一对多关系上使用级联删除规则和 validateForDelete

iphone - 如何自动调整 UIScrollView 的大小以适合其内容

iphone - 有没有办法找到设置了 AspectFit 的 UIImageView 内图像的调整大小高度/宽度?

iphone - UIBarButtonItem:setBackgroundVerticalPositionAdjustment 不能正常使用自定义按钮

iphone - 调用选择器时识别嵌入在自定义 UITableViewCell 中的 UISegmentedControl

iphone - 以特定方法访问NSManagedObjects时,应用程序崩溃