我有下面的代碼,它會觸摸一個按鈕並在該按鈕周圍繪製邊框,然後確保所有其他按鈕都沒有邊框(總共8個按鈕)。這是一個名爲AnswerButtons的單例類中的方法。此代碼工作正常。將一次性代碼轉換爲方法
- (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];
}
}
}
現在,我需要使用此代碼的所有8個按鈕,所以我應該寫的方法使用一個參數,該按鈕編號,以修改(POS)。在單例類的.m我就把這幾乎是相同的代碼:
- (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];
}
}
}
所以我改變了第一個代碼塊,使其對新方法的調用:
- (IBAction)button1WasTouched:(id)sender {
NSLog(@"Hello from button 1");
[sender activateAnswerAtPos:0];
}
不幸的是,我m做錯了,因爲我得到以下異常:
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
我不確定這裏發生了什麼;幾種替代方法都不起作用,而且我認爲我的故障排除方式正在向錯誤的方向發送我。我稱這種方法的方式有什麼問題?很明顯,我甚至沒有運行該方法。 TIA。
該死!很簡單。感謝你的快速回答,我又回來了。我很感激。 – 2012-03-31 00:00:57