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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 08-27-2009, 05:32 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 5
Question Use dynamic created NSString as UILabel name

Hello. I need your help. I want to chnage the textColor of a existing UILabel.

e.g.
theLabel1.textColor = [UIColor redColor];

The problem is, that I combine the label name "theLabel1" out of the String "theLabel" and a int "1" get from a loop.

NSString *labelName = [NSString stringbyformat:@"theLabel%@", number];

How can I now use the NSString labelName as object name to change the textColor dynamically?

labelName.textColor = ... Doesn't works.
[NSString stringbyformat:@"theLabel%@", number].textColor = ... Doesn't works.

HELP!

Thanks.
-Daniel
daniel.mac is offline   Reply With Quote
Old 08-27-2009, 05:36 AM   #2 (permalink)
iPhone Developer
 
kohjingyu's Avatar
 
Join Date: May 2009
Location: Singapore
Posts: 326
Default

Quote:
Originally Posted by daniel.mac View Post
Hello. I need your help. I want to chnage the textColor of a existing UILabel.

e.g.
theLabel1.textColor = [UIColor redColor];

The problem is, that I combine the label name "theLabel1" out of the String "theLabel" and a int "1" get from a loop.

NSString *labelName = [NSString stringbyformat:@"theLabel%@", number];

How can I now use the NSString labelName as object name to change the textColor dynamically?

labelName.textColor = ... Doesn't works.
[NSString stringbyformat:@"theLabel%@", number].textColor = ... Doesn't works.

HELP!

Thanks.
-Daniel
Hello,
You might want to use %i instead of %@. I don't know if that would work, but try it.

Also, you might want to use code tags when posting code, with [ code] and [ /code] without the spaces.

Hope that helps.
__________________
Bacteria Bash
Cheese Collect
Jokestar
Follow me on Twitter for news about my apps:
(Website|Twitter)
kohjingyu is offline   Reply With Quote
Old 08-27-2009, 07:47 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 792
Default

Throw the labels into an NSArray and loop it
nobre84 is offline   Reply With Quote
Old 08-27-2009, 08:03 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 5
Default

Quote:
Originally Posted by nobre84 View Post
Throw the labels into an NSArray and loop it
Have you got a code example for using the array elements as labelname? Thanks for reply.
daniel.mac is offline   Reply With Quote
Old 08-27-2009, 08:06 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 5
Default

Quote:
Originally Posted by kohjingyu View Post
Hello,
You might want to use %i instead of %@. I don't know if that would work, but try it.

Also, you might want to use code tags when posting code, with [ code] and [ /code] without the spaces.

Hope that helps.
Sorry, it was a typo. I use %i, but the problem seems to be the NSString themself. The compiler didn't translate it in the labelname respectively cann't use it. Thanks for reply.
daniel.mac is offline   Reply With Quote
Old 08-27-2009, 08:20 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 792
Default

Something like:
Code:
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: MY_LABEL_COUNT];
for (int i=0; i<MY_LABEL_COUNT; i++) {
[array addObject:[[UILabel alloc] initWithFrame:CGRectZero]];
}
You have an array full of labels now... and can iterate through them just like above, redeeming its object with [array objectAtIndex: ]

What exactly are you doing ? How do you create the labels ?
nobre84 is offline   Reply With Quote
Old 08-27-2009, 08:44 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 5
Default

Quote:
Originally Posted by nobre84 View Post
Something like:
Code:
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: MY_LABEL_COUNT];
for (int i=0; i<MY_LABEL_COUNT; i++) {
[array addObject:[[UILabel alloc] initWithFrame:CGRectZero]];
}
You have an array full of labels now... and can iterate through them just like above, redeeming its object with [array objectAtIndex: ]

What exactly are you doing ? How do you create the labels ?
I am creating a clock.app as my first iphone project. The labels are already created at Interface Builder. Now I want to change the color of the current minute... Therefore I use a "for... loop" and want to address the labels color for example.
Thanks for reply.
daniel.mac is offline   Reply With Quote
Old 08-27-2009, 12:20 PM   #8 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Send a message via AIM to Dutch
Default

This is never, ever, ever going to work. You are trying to get the textColor of an NSString object. There is no such thing.

Code:
[NSString stringbyformat:@"theLabel%@", number].textColor =
Your best bet is to assign each label a unique tag (which must be an integer).

Code:
for (NSInteger i=0; i<50; i++){
    UILabel *lbl=[[UILabel alloc] initWithFrame:CGRect(0,20*i, 100, 20)];
    [lbl setTag:i];
    [lbl setText:[NSString stringWithFormat:@"Label #%d", i]];
    [self.view addSubview:lbl];
    [lbl release];
}
This will give us 50 labels all with tags (0-49). Then later on, when you want to access the label, do something like this...
Code:
[self changeTextColorOfLabelWithTag:7 toColor:[UIColor greenColor]];
Code:
-(void)changeTextColorOfLabelWithTag:(NSInteger)theTag toColor:(UIColor *)clr{
    for (UIView *v is [self.view subviews]){
        if ([v isKindOfClass:[UILabel class]]){
            if ([v tag]==theTag){
                [(UILabel *)v setTextColor:clr];
                break;
            }
        }
    }
}

(NOTE THIS IS VIRTUALLY THE SAME THING AS CREATING AN ARRAY. THE TAG PROPERTY INDICATES ITS PSEUDO-INDEX IN AN IMAGINARY ARRAY)
Dutch is offline   Reply With Quote
Old 08-27-2009, 02:25 PM   #9 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 165
Default

How about creating an NSMutableDictionary to hold references to the labels:

Code:
NSMutableDictonary *labelDictionary = [[NSMutableDictonary alloc] init];
for (NSInteger i=0; i<50; i++){
    UILabel *lbl=[[UILabel alloc] initWithFrame:CGRect(0,20*i, 100, 20)];
    [lbl setTag:i];
    [lbl setText:[NSString stringWithFormat:@"Label #%d", i]];
    [self.view addSubview:lbl];
    [labelDictonary setObject:lbl forKey:lbl.text];
    [lbl release];
}
Now, whenever you want to get the label, all you have to do is

Code:
UILabel *lbl = (UILabel *)[labelDictionary objectForKey:[NSString stringWithFormat:@"Label #%d", i];
jonahgabriel is offline   Reply With Quote
Old 08-27-2009, 02:33 PM   #10 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 900
Default

Quote:
Originally Posted by Dutch View Post
Your best bet is to assign each label a unique tag (which must be an integer).

Code:
for (NSInteger i=0; i<50; i++){
    UILabel *lbl=[[UILabel alloc] initWithFrame:CGRect(0,20*i, 100, 20)];
    [lbl setTag:i];
    [lbl setText:[NSString stringWithFormat:@"Label #%d", i]];
    [self.view addSubview:lbl];
    [lbl release];
}
This will give us 50 labels all with tags (0-49). Then later on, when you want to access the label, do something like this...
This has the potential to get you into trouble because viewWithTag has to search through all the subviews looking for one that matches. If you're doing it a lot, it's a real performance hit.

Use an array or dictionary, as others have mentioned already in this thread.
jsd is offline   Reply With Quote
Old 08-27-2009, 02:48 PM   #11 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 792
Default

I like the Tag approach, very lightweight and functional
nobre84 is offline   Reply With Quote
Old 08-27-2009, 02:51 PM   #12 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 900
Default

Quote:
Originally Posted by nobre84 View Post
I like the Tag approach, very lightweight and functional
If you consider "lightweight" to be the same as "has the potential to make your app very slow"...
jsd is offline   Reply With Quote
Old 08-27-2009, 03:14 PM   #13 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Send a message via AIM to Dutch
Default

Quote:
Originally Posted by jsd View Post
If you consider "lightweight" to be the same as "has the potential to make your app very slow"...
Obviously, any programmer with any ability to see these things would have to realize that if you had 752,945 subViews on the searched form, the tag approach would be less than advisable. The right way to do this as you've said is to create an array and fill 'er up. I was just trying to demonstrate how you can not call an item by a dynamic name in the way the OP was trying.
Dutch is offline   Reply With Quote
Old 08-27-2009, 03:56 PM   #14 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 792
Default

If he has a subview filled only with buttons wouldn't the performance be the same of the array without the need to create it?
But in fact I didn't stop to think about the inner working of viewWithTag and the very bad scenarios that could happen
nobre84 is offline   Reply With Quote
Old 08-27-2009, 04:12 PM   #15 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Send a message via AIM to Dutch
Default

Quote:
Originally Posted by nobre84 View Post
If he has a subview filled only with buttons wouldn't the performance be the same of the array without the need to create it?
But in fact I didn't stop to think about the inner working of viewWithTag and the very bad scenarios that could happen
Actually, using an array you can access the objects directly giving its array index to [array objectAtIndex:...].... This eliminates the need to FOR loop through them all. Again, if we're talking about 5, 10, 25 objects either is fine - but for larger numbers I would use the array.
Dutch is offline   Reply With Quote
Old 08-28-2009, 08:06 AM   #16 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 792
Default

Ouch, now I see what you mean.
I assumed viewWithTag was some magical hash based search when it actually sweeps each subview to find the first matching tag.
nobre84 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsstring, uilabel

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: 251
22 members and 229 guests
14DEV, @sandris, ADY, ArtieFufkin10, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, prchn4christ, Rudy, ryantcb, Speed, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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