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 11-23-2010, 12:49 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 8
vatsa.devil is on a distinguished road
Question Generating variable Names @ runtime

is it possible to generate variables at runtime? something like

Code:
for(i=0;i<5;i++){
  NSString *string+i;
}
What i'm trying to do is this. I have a rating screen in my app. I have given tag values of 1 to 5 for all the five star buttons (StarButton1,StartButton2...startButton5). what i intend to do is, when a user presses, lets say 3rd star, all the 'stars' till 3 (i.e 1,2 and 3), should change its background color. The following function is connected to all the buttons..

Code:
-(void)starPressed:(UIButton*)sender{
NSLog(@"Button pressed is %d",sender.tag);//this shows the correct button No.
for(int i=1;i<sender.tag;i++){
//below line shows sort of what i'm trying to do. You get the point right. 
//What would be the correct way to do this??
[StarButton+i setBackgroundImage:coloredStar forState:UIControlStateNormal];
}
}
Any help?? Or any other way of doing this??

Last edited by vatsa.devil; 11-23-2010 at 12:51 AM. Reason: to make the question more clear
vatsa.devil is offline   Reply With Quote
Old 11-23-2010, 01:56 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

As far as I know, there's no way to "number" your pointer variable names at runtime.

There are two things you can do, though. First is to use an array. Instead of trying to number your buttons' pointers, put them into an NSMutableArray and reference their "numbers" that way.

Code:
self.starBtnArray = [NSMutableArray arrayWithCapacity:5]; //starBtnArray should be a "retain" NSMutableArray property!
for(i=0;i<5;i++){
  StarButton *starBtn = [[StarButton alloc] init];
  [starBtnArray addObject:starBtn];
  starBtn.tag = i; //For good measure
  [starBtn release]; //Bring retain count back down to 1
}
Code:
for(int i=0;i<sender.tag;i++){
  [[starBtnArray objectAtIndex:i] setBackgroundImage:coloredStar forState:UIControlStateNormal];
}
Make sure you learn about properties if you don't already understand them...they're very important for objective-C coding. Also make sure you note the change to 0 in the first part of the for loop expressions in the second bit of code, since array indices start at 0.

The second way is to use UIView's viewWithTag: method which returns a view's subview that has the tag.

Code:
for(int i=1;i<sender.tag;i++){ //I'm assuming this is in your view controller?
  [[self.view viewWithTag:i] setBackgroundImage:coloredStar forState:UIControlStateNormal];
}
I wouldn't personally prefer this way because then you have to manage and remember to call release for 5 pointers (one for each button) instead of just one (an NSMutableArray). In addition, you can run into trouble if you try to have more than one view with a tag number from 1-5 as viewWithTag only returns one UIView.

It's up to you though, whichever you're more comfortable with.
__________________
HEY! Was this post helpful?
If so, it would be MUCH appreciated if you'd just click on one of these apps:



MyD
Take 1 minute to set up your MyD and you'll always be able to prove you own your device!

Membrik
Test your memory by sliding tiles to match chains of increasing difficulty.

©2011 Dardom Productions | Like us on Facebook!
SoulRed12 is offline   Reply With Quote
Old 11-23-2010, 03:56 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 8
vatsa.devil is on a distinguished road
Default

Quote:
Originally Posted by SoulRed12 View Post
There are two things you can do, though. First is to use an array. Instead of trying to number your buttons' pointers, put them into an NSMutableArray and reference their "numbers" that way.
Now why didn't I think of that?? That completely solves my problem i guess. Thanks a lot for the reply..

PS: about your signature, Your reply was indeed helpful and i do have $0.99, but i don't have an iphone
vatsa.devil is offline   Reply With Quote
Old 11-23-2010, 12:05 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

Quote:
Originally Posted by vatsa.devil View Post
Now why didn't I think of that?? That completely solves my problem i guess. Thanks a lot for the reply..

PS: about your signature, Your reply was indeed helpful and i do have $0.99, but i don't have an iphone
lol it's alright, and glad I could help
__________________
HEY! Was this post helpful?
If so, it would be MUCH appreciated if you'd just click on one of these apps:



MyD
Take 1 minute to set up your MyD and you'll always be able to prove you own your device!

Membrik
Test your memory by sliding tiles to match chains of increasing difficulty.

©2011 Dardom Productions | Like us on Facebook!
SoulRed12 is offline   Reply With Quote
Reply

Bookmarks

Tags
dynamic, runtime, variablename

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: 376
7 members and 369 guests
apatsufas, Kirkout, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

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