Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-13-2010, 11:07 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 5
StrikeAnywhere is on a distinguished road
Question help with a concept - variable variable name? key-value coding?

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.
StrikeAnywhere is offline   Reply With Quote
Old 09-14-2010, 05:23 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 47
royen is on a distinguished road
Default

Hi!

Unless I'm gravely mistaken, what you're looking for is the valueForKey: method. Something like this ought to work, assuming you have an ivar that corresponds to "flag%d":

Code:
// ...
NSString *key = [NSString stringWithFormat:@"flag%d", flagNum];
[[NSUserDefaults standardUserDefaults] setObject:[self valueForKey:key] forKey:@"buttonFlag"];
// ...
royen is offline   Reply With Quote
Old 09-14-2010, 05:46 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Just use the tag property of the button. Create each button with a unique tag (it is an integer). Use the same responder method for all the buttons, but customize the text of the alert view based on the tag of the button pressed.
RLScott is offline   Reply With Quote
Old 09-14-2010, 10:45 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 5
StrikeAnywhere is on a distinguished road
Default

Thanks you two! Been working today, so I haven't had time to try that out but what you said royen looks like what I was trying to do. I'll take a stab at it first thing in the morning.

I didn't even know buttons could have tags either. So I guess I better explore that too!

Thanks so much!
StrikeAnywhere is offline   Reply With Quote
Old 09-16-2010, 01:49 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 5
StrikeAnywhere is on a distinguished road
Default

So this seems to work how I wanted it to!

Quote:
NSString *key = [NSString stringWithFormat:@"flag%d", flagNum];
NSString *key2 = [NSString stringWithFormat:@"buttonFlag%d", flagNum];
[[NSUserDefaults standardUserDefaults] setObject:[self valueForKey:key] forKey:key2];
I set up a label on the page that changes on button press, and the text on the label updates correctly, yay! Before I move on and call it a victory, is there any reason to believe that the part in red above isn't putting the value of flag1 into the NSUserDefault buttonFlag1, and rather creating a new default called key2?


I also would like to replace the line flag = @"none2.png"; by using the value of key in place of flag, so that it could be flag1 or flag2 depending on button pressed. Replacing flag with key in the line just makes the app crash... How do I go about doing that? I tried contactenation, but this doesn't seem to be the right way of doing it either.
StrikeAnywhere is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 331
13 members and 318 guests
Absentia, cgokey, fiftysixty, givensur, heshiming, iGamesDev, linkmx, michaelhansen, mraalex, PixelInteractive, raihan.zbr, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,117
Posts: 402,891
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:08 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0