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, 05:26 PM   #1 (permalink)
Creator of From A to B
 
fudgie_no1's Avatar
 
Join Date: Nov 2010
Location: Wales
Posts: 45
fudgie_no1 is on a distinguished road
Unhappy Set Background Image Using String as button identifier

Hi all, I've been searching the net all day and still haven't found the code I need! I fear it's impossible and I'll have to code it all the long way.

Basically, I have a formula that calculates the correct button name I wish to use (C2R8 for example). This name is stored as an NSString (called MyButton). What I'd like to do is to be able to use the NSString (MyButton) as an identifier instead of the declared column name (C2R8). So for example:

instead of writing:
[C2R8 setImage:[UIImage imageNamed:@"Blank.PNG"] forState:UIControlStateNormal];

I could write:
[MyButton setImage:[UIImage imageNamed:@"Blank.PNG"] forState:UIControlStateNormal];

surely this can be done? It would save me hours of pointless coding! Thanks for your time.
fudgie_no1 is offline   Reply With Quote
Old 11-23-2010, 07:12 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 386
SoulRed12 is on a distinguished road
Default

You don't have to do all that. You can just use NSMutableArrays. Store your matrix of buttons in NSMutableArrays stored in other NSMutableArrays. Visually, it might look like this:

{
{r1c1, r1c2, r1c3}
{r2c1, r2c2, r2c3}
{r3c1, r3c2, r3c3}
{r4c1, r4c2, r4c3}
...etc...
}

You have the overall NSMutableArray, that you call "buttonsArray" for example, and numerous NSMutableArrays within it that serve as the rows (and the individual objects within each internal NSMutableArray serve as columns).

This way instead of creating a string to correspond to a particular button (which I don't believe can work anyways), you just handle array indices. For example, to get the button at row 5, column 8, (starting at 0 for each) you'd use:

[[[buttonsArray objectAtIndex:4] objectAtIndex:7] setImage:...];

Hopefully that makes sense.
__________________
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, 09:38 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by SoulRed12 View Post
You don't have to do all that. You can just use NSMutableArrays. Store your matrix of buttons in NSMutableArrays stored in other NSMutableArrays. Visually, it might look like this:

{
{r1c1, r1c2, r1c3}
{r2c1, r2c2, r2c3}
{r3c1, r3c2, r3c3}
{r4c1, r4c2, r4c3}
...etc...
}

You have the overall NSMutableArray, that you call "buttonsArray" for example, and numerous NSMutableArrays within it that serve as the rows (and the individual objects within each internal NSMutableArray serve as columns).

This way instead of creating a string to correspond to a particular button (which I don't believe can work anyways), you just handle array indices. For example, to get the button at row 5, column 8, (starting at 0 for each) you'd use:

[[[buttonsArray objectAtIndex:4] objectAtIndex:7] setImage:...];

Hopefully that makes sense.

I agree with SoulRed. Just use arrays. In fact, if the size of the grid of buttons is small, and never goes above a small value, I'd probably use a hard-coded C 2-dimensional array. I think that code would be simpler and easier to read, but then I'm an old C jockey.

Something like this:

Code:
UIButton* buttonArray[5, 3];
(I think that's the syntax for a statically allocated 2 dimensional C array, but I'd need to cheek a manual to be sure.)

Then I would probably use tags for my buttons where the 100s place of the tag is the row, and the tens/ones place is the column, and set up the button array using code like this:

Code:
UIButton* aButton;
for (int row = 0; row<5; row++)
  for (int column =0; column<3; column++)
  {
    int tag = column*100 + row;
    aButton = [self.view viewWithTag: tag];
    buttonArray[row, column] = aButton;
  }
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 11-24-2010, 01:51 AM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

There's no need to create a new thread for the same question as you have here http://www.iphonedevsdk.com/forum/ip...namically.html
baja_yu is offline   Reply With Quote
Old 11-24-2010, 04:03 AM   #5 (permalink)
Creator of From A to B
 
fudgie_no1's Avatar
 
Join Date: Nov 2010
Location: Wales
Posts: 45
fudgie_no1 is on a distinguished road
Unhappy

Ye sorry, I just thought by using a different title more people would understand what I'm trying to do and could offer advice. I have a better understanding of what needs to be done as a result, nsmutablearrays is definately the way forward (as you've tried to hep me with in the other thread) but I just can't get them working. I've decided to hard code all the different possibilities now and I may teach myself how to use nsmutablearrays in the future. Thank you all for your time
fudgie_no1 is offline   Reply With Quote
Reply

Bookmarks

Tags
control, nsstring, reference, 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: 377
5 members and 372 guests
Kirkout, 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