I have the following code which takes a touch on one button and draws a border around that button, then makes sure that all the other buttons have no border (8 buttons total). This is a method in a singleton class called AnswerButtons. This code works fine.
- (IBAction)button1WasTouched:(id)sender {
NSLog(@"Hello from button 1");
// retrieve, modify and update clueAnsState
NSMutableArray *newCAS = [[GameData gameData].curData objectForKey:@"clueAnsState"];
[newCAS replaceObjectAtIndex:0
withObject:@"2"];
[[GameData gameData].curData setObject:newCAS
forKey:@"clueAnsState"];
// Highlight the pressed button & make sure other buttons are not highlighted
for (NSInteger idx = 0; idx < 8; idx++) {
NSString *temp = [newCAS objectAtIndex:idx];
if ([temp isEqualToString:@"1"]) {
UIButton *b = [[AnswerButtons answerButtons].buttons objectAtIndex:idx];
[[b layer] setBorderWidth:0.0f];
}
if ([temp isEqualToString:@"2"]) {
UIButton *b = [[AnswerButtons answerButtons].buttons objectAtIndex:idx];
[[b layer] setBorderWidth:2.0f];
}
}
}
Now, I need to use this code for all 8 buttons, so I should write a method with one argument, the button number to modify (pos). In the singleton class .m I put which is virtually the same code:
- (void)activateAnswerAtPos:(int)pos {
// retrieve, modify and update clueAnsState
NSMutableArray *newCAS = [[GameData gameData].curData objectForKey:@"clueAnsState"];
[newCAS replaceObjectAtIndex:pos
withObject:@"2"];
[[GameData gameData].curData setObject:newCAS
forKey:@"clueAnsState"];
NSLog(@"%@", newCAS);
for (NSInteger idx = 0; idx < 8; idx++) {
NSString *temp = [newCAS objectAtIndex:idx];
if ([temp isEqualToString:@"1"]) {
UIButton *b = [[AnswerButtons answerButtons].buttons objectAtIndex:idx];
[[b layer] setBorderWidth:0.0f];
}
if ([temp isEqualToString:@"2"]) {
UIButton *b = [[AnswerButtons answerButtons].buttons objectAtIndex:idx];
[[b layer] setBorderWidth:2.0f];
}
}
}
So I changed the first code chunk to make it a call to the new method:
- (IBAction)button1WasTouched:(id)sender {
NSLog(@"Hello from button 1");
[sender activateAnswerAtPos:0];
}
Unfortunately, I’m doing something wrong as I get the following exception:
2012-03-30 19:41:40.199 P3[6751:f803] Hello from button 1
2012-03-30 19:41:40.201 P3[6751:f803] -[UIRoundedRectButton activateAnswerAtPos:]: unrecognized selector sent to instance 0x6e709f0
I’m not sure what’s going on here; several alternatives don’t work either and I think my troubleshooting is sending me in the wrong direction. What’s wrong with the way I am calling this method? Clearly I’m not even getting to run the method. TIA.
You’re calling
-activateAnswerAtPos:on sender, which is the button that was touched. You should instead call it on the instance of the class that defines the-activateAnswerAtPos:method. It’s not clear from your code what that is, but my guess is self: