2010-02-12 78 views
0

我這裏有一個方法,那就是在我的代碼:爲什麼我的方法不能正常工作? (iPhone SDK)

-(IBAction) actionButtonPressed: (id) sender{ 
    NSLog(@"%@",actionButton.titleLabel.text); 
    if (actionButton.titleLabel.text == @"Begin Recording"){ 
     [actionButton setTitle:@"Finish Recording" forState:UIControlStateNormal]; 
     [actionButton setTitle:@"Finish Recording" forState:UIControlStateApplication]; 
     [actionButton setTitle:@"Finish Recording" forState:UIControlStateHighlighted]; 
     [actionButton setTitle:@"Finish Recording" forState:UIControlStateReserved]; 
     [actionButton setTitle:@"Finish Recording" forState:UIControlStateSelected]; 
     [actionButton setTitle:@"Finish Recording" forState:UIControlStateDisabled]; 
     UIImage *redButton = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bigredbutton" ofType:@"png"]]; 
     [actionButton setBackgroundImage:redButton forState:UIControlStateNormal]; 
     [actionButton setBackgroundImage:redButton forState:UIControlStateApplication]; 
     [actionButton setBackgroundImage:redButton forState:UIControlStateHighlighted]; 
     [actionButton setBackgroundImage:redButton forState:UIControlStateReserved]; 
     [actionButton setBackgroundImage:redButton forState:UIControlStateSelected]; 
     [actionButton setBackgroundImage:redButton forState:UIControlStateDisabled]; 
    } 
    if (actionButton.titleLabel.text == @"Finish Recording"){ 
     [self dismissModalViewControllerAnimated:YES]; 
    } 

} 

出於某種原因,NSLog的調用不工作並顯示在控制檯。該按鈕開始讀取開始記錄,但按下它卻什麼都不做,即使它與Interface Builder的Touch Up Inside調用此方法並被引用。

回答

3

你的問題是這樣的一行:

if (actionButton.titleLabel.text == @"Begin Recording"){ 

有你比較指針,不是字符串。你需要:

if ([actionButton.titleLabel.text isEqualToString:@"Begin Recording"]){ 
+1

這是最正確的,但我認爲你應該使用isEqualToString,而不是isEqual。爲了擴大Dave所說的內容,在比較常量字符串時,==運算符只會計算爲真,並且以這種方式使用它仍然是一種糟糕的形式。對所有字符串比較使用isEqualToString。 – 2010-02-12 21:44:42

+0

@Andrew - 夠公平的。編輯答案。 (雖然我從來沒有使用過「isEqual:」的問題) – 2010-02-12 21:49:53

+0

是的,我認爲是Equal會工作,因爲它是你說的,但我從來沒有用過它...我不知道他們是否區別。 – 2010-02-12 22:55:27