I am looking into how to access a variable within my class using text as its name. The only solution I have found so far to do so is using an NSDictionary and assigning that particular object a key. Using this method, I have added eight UIButtons (declared in the header as: 'IBAction UIButton *button1' etc...) and assigned them keys (such as: 'button1, @"button1s", button2, @"button1d", ...') in a new NSDictionary that is global and is named buttonsDict. I am now trying to access and change a property in a button in that NSDictionary based on the name of a button created in the Interface Builder's title. The titles are all following this example: '1s'. Once appending the word 'button' to the string of the title of the button it should return something like: 'button1s'. The method I am trying to do this is as follows:
Code:
-(IBAction)buttonPress:(id)sender {
NSString *temp = @"button";
temp = [temp stringByAppendingString:[sender titleForState:UIControlStateNormal]];
UIButton *button = [buttonDict objectForKey:temp];
button.hidden = YES;
[buttonDict setValue:button forKey:temp];
}
I am relatively new at iPhone programming and am not sure why the run of this particular program exits with message 'EXC_BAD_ACCESS' at the line of the creation of 'button' when I press any button in the simulator. Any ideas?
Attached are the class files Where this method came from, if that helps...