Hello
I am just learning objective-C and I'm having trouble wrapping my head around a concept, so I was hoping someone would be able to help me.
I created a little test app to try out a couple of concepts:
I have a button that pops up a UIAlertView that contains a UISegmentedControl bar with 5 buttons on it, each containing a small picture. Pressing one of those buttons sets that picture on the button pressed and saves it to an NSUserDefault so that if you close the app and come back in the selection is still there.
So far everything works like I wanted it too and life is great.
But now I wanted to add a second, third, twenty first whatever button to do the same thing, and I don't want to have to re-write the code from the UIAlertView and the SegmentedControl. So instead, i want to set a variable when whichever button is pressed, and then use that number as part of another variable to set the info for that button... and I can't figure out how to do that.
As an example, here's a piece of the segemntedcontrol code:
Quote:
if (segment.selectedSegmentIndex == 0) {
flag = @"none2.png";
[[NSUserDefaults standardUserDefaults] setObject:flag forKey:@"buttonFlag"];
[[NSUserDefaults standardUserDefaults] synchronize];
[button setImage:imagen forState:UIControlStateNormal];
}
|
So, if when a button is pressed I set an int named flagNumber to 2 for example. Can I change that variables 'flag', button and and buttonFlag to be 'flag'+'flagNum' (etc.) so that flag2 is set, and and then the value of flag2 is inserted into the NSUserDefault buttonFlag2, and the image is set on button2?
In doing research on the problem I've come across a couple of posts or articles referencing key value coding, so I tried reading through the section on that in the Mac Reference Library but I am not getting it, and I'm not even totally sure it's what I'm looking for.
An example or a pointer in the direction of a tutorial would be super awesome.
And thanks to everyone on the site for your super helpful info. I've managed to learn what I know so far from this site and some youtube tutorials.