Conditionals
A conditional (WOConditional) is a dynamic container element that displays its contents only if a particular condition is true. WOConditional's main attribute is condition, which takes a Boolean value. If condition is true (1), the WOConditional's contents are displayed. If condition is false (0), the contents aren't displayed.condition can be bound to a variable or to method that returns a Boolean value. (WebScript and Objective-C use the constants YES and NO; Java uses true and false.) To bind condition (or any other attribute that takes a Boolean) to a constant value, enter YES or NO in the bindings Inspector.
To create a conditional, click in the toolbar.
Note: Any selected elements will be contained within the conditional.
To bind to a conditional, click a variable or method and drag to one of the conditional's outer icons. The Inspector appears, displaying the bindings for the WOConditional, with the condition attribute selected by default. Complete the binding by clicking Connect Variable, or choose a different attribute to bind.
There is a shortcut for binding the condition attribute. Drag from a key in the object browser to the binding box in the conditional.
Sometimes, you want the equivalent of an "if-then-else" structure; that is, "if the condition is true, display this text; if not, display this other text." To accomplish this, you can use the negate attribute. If negate is true, then the contents of the conditional are displayed only if condition is false. To create an if-then-else structure, do the following:
- Create two WOConditionals.
- Bind the condition attribute of both of them to the same variable or method.
- Bind the negate attribute of the second one to YES (true).
By default, negate is false, so you do not explicitly need to bind the first conditional's negate attribute.