Flavouring Objective-C iOS app with Storyboard



我在Objective-C中有一个带有Storyboard的单活动iOS应用程序。该应用程序有两个构建方案,例如方案 1 和方案 2。视图只有几个按钮。我想根据构建方案区分这些按钮的颜色。

我是 iOS 世界的新手,但我很难根据构建方案对故事板(例如颜色、字符串等(进行参数化。我知道这篇文章,但我想要更明确的东西。

谢谢你的帮助。

Xcode 不支持使用 Scheme 进行条件编译。为此,您需要维护两个目标,这很快就会变得混乱。

要使用 Scheme 执行此操作,您需要维护两个具有正确命名颜色的资产目录,并在构建时复制正确的资源目录。源.xcasset目录不会添加到目标中。

需要尽早在目标的"生成阶段"部分添加运行脚本

幸运的是,方案名称是通过配置环境变量表示的。您可以执行以下操作,您的路径可能会有所不同:

# Copy over the appropriate asset catalog for the scheme
target=${SRCROOT}/Resources/Colors.xcassets
if [ "${CONFIGURATION}" = "Scheme 1" ]; then
sourceassets=${PROJECT_DIR}/Scheme1.xcassets
else
sourceassets =${PROJECT_DIR}/Scheme2.xcassets
fi
if [ -e ${target} ]; then 
echo "Assets: Purging ${target}"
rm -rf ${target}
fi
echo "Assets: Copying source=${sourceassets} to destination=${target}"
cp -r ${sourceassets} ${target}

实质上,您正在将资产目录的编译版本替换为您的 Scheme 特定版本之一。

字符串将是另一个问题,对于本地化字符串,您可以使用相同的技术来执行此操作。

这一切都很快变得可怕,不建议这样做。使用参考帖子中描述的技术在运行时通过代码配置 UI 将是一个更好的选择。

您可以构造一个填充层来保护您的代码免受方案更改的影响。

例如

@interface MyColors : NSObject
+ (UIColor *)buttonBackground;
@end
@implementation MyColors
+ (UIColor *)buttonBackground {
#if SCHEME1
return [UIColor colorNamed:@"scheme1ButtonBackground"];
#else
return [UIColor colorNamed:@"scheme2ButtonBackground"];
#endif
}
@end 

最新更新