繁体   English   中英

使用 Storyboard 调味 Objective-C iOS 应用程序

[英]Flavouring Objective-C iOS app with Storyboard

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

我是 iOS 世界的新手,但我很难根据构建方案参数化 Storyboard(例如 colors、字符串等)。 我知道这篇文章,但我想要更明确的东西。

谢谢您的帮助。

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

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

您需要尽早在目标的“构建阶段”部分添加运行脚本

幸运的是,方案名称是通过CONFIGURATION环境变量表示的。 你可以这样做,你的路径可能会有所不同:

# 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 是一个更好的选择。

您可以构建一个 shim 层来保护您的代码免受 Scheme 更改的影响。

例如

@interface MyColors : NSObject

+ (UIColor *)buttonBackground;

@end

@implementation MyColors

+ (UIColor *)buttonBackground {

#if SCHEME1
    return [UIColor colorNamed:@"scheme1ButtonBackground"];
#else
    return [UIColor colorNamed:@"scheme2ButtonBackground"];
#endif

}

@end 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM