So I am implementing a custom "chooser" toolbar, like the iOS equivalent of a radio button set (UISegmentedControl
). Just a horizontal bar divided into options.
To do this, I created a subclass of UIControl
called SegmentedControl
and implemented custom drawing. However, with such a view, I need the option to set what the available options are. I could have just accessed the view from the controller's viewDidLoad()
and set those there, but I like using the interface builder for that kind of stuff.
So I discovered this wonderful thing called "User Defined Runtime Attributes." I created a String
attribute with a key buttonValues
and set a value (this is a simple Male/Female chooser so I went with "Male|Female"). I found out that you can access these values using the function self.valueForKey()
and pass in the key. I made a parser to turn that string into an array and then added functionality for the drawRect()
function to use the array to set up the buttons.
When I ran the app, I got an error about "Key Value Coding-compliance."
So I looked that up, and I found out that the class has to have backing variables to store the attributes. So fine, I added an instance variable called buttonValues
and initialized it to ""
. Now the app runs fine but the value comes out empty from the self.valueForKey()
function. I looked up tutorials on how to set up user defined runtime attributes but they don't go into enough detail. They talk about Key Value Coding-compliance like it's something I should just know.
I would like to know exactly what I must do for this to work properly, in gory detail.