This post is about reducing your controller lines of code, and moving some stuff in the Interface Builder.
If you already know why this is for and why setting this attributes can't be read in code you can skip this post. <!-- more -->
Let's say you have a form with 20 text fields, and various other inputs. When the text field looses focus, you want to save it's value to the database. When you open that screen later again, you want all the values preloaded.
A common way of doing it if you had 2-3 inputs, would be:
1 2 3
and then in your code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Logically, if you had 20 of this inputs, your controller would get bloated. A lot. The second problem is, if you change the database field, you need to change the controller (and probably the view). That's violating the SRP principle.
Wouldn't it be great if you could give those fields the same identifier as the database fields are called?
For instance, if a Person has
surname ,... then your fields identifiers should be
If you can manage that, than the above methods could become very short.
1 2 3 4 5 6 7 8 9
Let's say we want to make a small hack, and use the Accessibility (label, hint, identifier) for
You can set these values in the IB,
but you can't read them in the code.
1 2 3 4
The weird part is, if you set them in the code, it works like a charm.
1 2 3 4
I'm not advocating the usage of accessibility-stuff.
You should probably subclass the
UI*Component and that way document your code better, open some new possibilities, etc.
Let's say I've made a FormTextField, with 2 properties:
required , and
In that case the IB would look like:
To sum up: