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-16-2010, 01:03 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 6
BAR115 is on a distinguished road
Post Accessing a UIButton using a string

I am trying to use a string to access a UIButton.

- (void) setup: (NSString *) hello {

if (peices[1] == 0) { [(hello.value) setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal]; }

}

I have "(hello.value)" in the place i want to have a UIButton name but instead I would like it to read the value of the string as the name of the UIButton, and then it would obviously set the picture of the button to nil.

Please help, I am fairly new to objective-c.
BAR115 is offline   Reply With Quote
Old 09-16-2010, 01:38 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 50
o2fill is on a distinguished road
Default

When you same button "name", do you mean title (i.e. the text it displays)? And are you trying to identify which button based on what the title is? Assuming the answer to both is yes, I think this might do the trick:

[code]]
for (UIButton *button in yourView.subviews) {
if (button.titleLabel.text == hello) {
[button setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
}
[/CODE

Good luck,

Cheers
o2fill is offline   Reply With Quote
Old 09-16-2010, 01:46 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 6
BAR115 is on a distinguished road
Default

Quote:
Originally Posted by o2fill View Post
When you same button "name", do you mean title (i.e. the text it displays)? And are you trying to identify which button based on what the title is? Assuming the answer to both is yes, I think this might do the trick:

[code]]
for (UIButton *button in yourView.subviews) {
if (button.titleLabel.text == hello) {
[button setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
}
[/CODE

Good luck,

Cheers
Actually thats not exactly what i needed...
You see how you have declared your UIButton as "button". Instead of directly having typed in "button" i would like to set the name of the button as the value of a string. I have like 50 buttons on a view and it would be easy to set the image of all of them by just changing the number on the button and then changing the image. Like - button1, button2, button3... etc just save button as a string and then add numbers on to the end to set the pictures.
BAR115 is offline   Reply With Quote
Old 09-16-2010, 02:21 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 50
o2fill is on a distinguished road
Default Got it

OK - I get you now, you mean that the variable name is given by the string hello. No idea. Here are a few suggestions in case you're stuck.

It would be much easier to add all the buttons programmatically and just run through the array. Or, if you added them through IB, you could run through the subviews array. ORr, you could enter a value for each button in the "tag" field in the IB Inspector and then search the array for that tag.

Anyway - good luck. And, if you find a way to do this please post it here.

Quote:
Originally Posted by BAR115 View Post
Actually thats not exactly what i needed...
You see how you have declared your UIButton as "button". Instead of directly having typed in "button" i would like to set the name of the button as the value of a string. I have like 50 buttons on a view and it would be easy to set the image of all of them by just changing the number on the button and then changing the image. Like - button1, button2, button3... etc just save button as a string and then add numbers on to the end to set the pictures.
o2fill is offline   Reply With Quote
Old 09-16-2010, 08:14 PM   #5 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

I'm trying to follow this, and my interpretation is that you want to access the variable with the same name as an NSString. Fortunately, all views conform to KVC (key-value coding). Therefore, you can simply call

[self setValue:buttonInstanceWithCustomizedImage forKey:buttonNameAsNSString];

I think this is exactly what you're looking for...hope it helps!
__________________
If I have helped you, please consider donating. I use PayPal. It would mean a lot to me!


For more iOS Development, check out my blog—Cups of Cocoa. All visitors welcome, but especially beginners!

Hope I have helped!

Please check out IceFall, a new action game available in the App Store!
musicwind95 is offline   Reply With Quote
Reply

Bookmarks

Tags
accessing, declaration, string, uibutton

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: 309
16 members and 293 guests
2ndSegment, cayladv57, cgokey, dermotos, djohnson, Domele, Hamad, heshiming, linkmx, markuschow, pungs, Rudy, Sloshmonster, teebee74, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

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