2012-03-30 20 views
0

我有下面的代碼,它會觸摸一個按鈕並在該按鈕周圍繪製邊框,然後確保所有其他按鈕都沒有邊框(總共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。

回答

1

您在發件人處打電話-activateAnswerAtPos:,這是被觸摸的按鈕。您應該在定義-activateAnswerAtPos:方法的類的實例上調用它。這不是從你的代碼是什麼是明確的,但我的猜測是自:

[self activateAnswerAtPos:0]; 
+0

該死!很簡單。感謝你的快速回答,我又回來了。我很感激。 – 2012-03-31 00:00:57

2

很難跟隨你正在嘗試做的,但我可能會發送所有按鈕動作一個方法是這樣

- (void)buttonTapped:(UIButton *)buttonTapped; 
{ 
    NSArray *buttons = [AnswerButtons answerButtons].buttons; 

    // some kind of switch statement of logic to perform options depending on which button 
    // Can use the following to get the index 
    // NSInteger buttonIndex = [buttons indexOfObject:buttonTapped] 

    for (UIButton *button in buttons) { 
     if (button == buttonTapped) { 
      // highlight 
     } else { 
      // remove highlight 
     } 
    } 
} 
+0

謝謝保羅。我的問題解決了你寫作的時間,但你的代碼顯示了我做了一些比我一直在做的事情更精簡的方法,所以這是一個獎金。 – 2012-03-31 00:25:41