Search code examples
iosxcodemacosinterface-builderxib

Is there substitution text syntax for strings in Xcode interface builder UI?


I have an Objective C++ program used to handle the setup of our different applications. Is there a way to use preprocessor defines to create text to be substituted in the strings used by NSTextFieldCell, NSButtonCell?

FOR EXAMPLE, instead of have an NSTextField that says "Options for setting up Foo", there would be a preprocessor macro (GCC_PREPROCESSOR_DEFINITIONS):

MY_PROGRAM_NAME=Bar

and then the text for NSTextField would be:

"Options for setting up $(MY_PROGRAM_NAME)"

Which would then have the desired result: "Options for setting up Bar"

NOTE 1: obviously, I could do the substitution programmatically in code.

Note 2: this is for Xcode 7, so perhaps there isn't a feature like this?


Solution

  • In a word, no. The Xcode nib compiler doesn't perform any kind of variable substitution and—once encoded—all archived property values are static.

    However, if this is a "thing" for you application, and there aren't too many view classes involved (say, just NSTextField), it wouldn't be hard to roll your own solution.

    I'd consider this approach:

    • Concoct a simple-to-substitute syntax, a la "Some string {VAR_NAME}".
    • Define your variables as key/value pairs in a dictionary. Store the dictionary as an XML file / dictionary in the app bundle.
    • At app startup, load the dictionary and make it public by putting it in a global variable or adding it to -[NSUserDefaults registerDefaults:]
    • Subclass NSTextField (as an example). Override either -initWithCoder: or -awakeFromNib. In the override, get the string value of the view object, scan it for substitutions using the public variable dictionary, and update the string property as appropriate.
    • In IB, change the class of any NSTextField that needs this feature to your custom subclass.