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.
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:
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:
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:
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:
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;
}
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.
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