2012-07-23 53 views
1

我是新來的Objective-C,我已經通過了教程打造「小費計算器」應用爲iPhone。我正在使用Xcode 4.3.3並且我已經檢查了所有的代碼,它與本教程中的相同(我確信它是正確的)。我一直在通過網站搜尋,試圖找到有同樣問題的人,但我無法修復它,所以我非常感謝任何幫助。這是我嘗試運行應用時出現的消息。的Objective-C:iPhone應用程序線程1個信號SIGABRT

2012-07-23 14:22:01.021 Tip_Calculator [554:F803] - [視圖控制器 的initWithCoder:]:無法識別的選擇發送到實例0x68a3130 2012-07-23 14:22:01.025 Tip_Calculator [554 :F803] *終止應用程序 由於未捕獲的異常 'NSInvalidArgumentException',原因是: ' - [視圖控制器的initWithCoder:]:無法識別的選擇發送到 例如0x68a3130' *第一擲調用堆棧:(0x13c8022 0x1559cd6 0x13c9cbd 0x132eed0 0x132ecb2 0x234135 0x333c6e 0x333383 0x233cad 0x333c6e 0x33367b 0x333383 0x233105 0x43ceef 0x43d03e 0x11d7a 0x11ff8 0x1117 ˚F0x20183 0x20c38 0x14634 0x12b2ef5 0x139c195 0x1300ff2 0x12ff8da 0x12fed84 0x12fec9b 0x10c65 0x12626 0x253d 0x24a5)終止叫做拋出 異常(LLDB)

這是main.m文件標記的區域:

#import <UIKit/UIKit.h> 

#import "AppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
@autoreleasepool {  return UIApplicationMain(argc, argv, nil,  NSStringFromClass([AppDelegate class])); } 
} 

我檢查了所有的接口連接,它們都很好。我還添加一個異常斷點和這個錯誤出現了:

2012-07-23 20:49:53.633 Tip_Calculator [2067:F803] - [視圖控制器 的initWithCoder:]:無法識別的選擇發送到實例0x68c09c0 (LLDB)

這是顯示的錯誤日誌:

Tip_Calculator`start: 
0x2470: pushl $0 
0x2472: movl %esp, %ebp 
0x2474: andl $-16, %esp 
0x2477: subl $16, %esp 
0x247a: movl 4(%ebp), %ebx 
0x247d: movl %ebx, (%esp) 
0x2480: leal 8(%ebp), %ecx 
0x2483: movl %ecx, 4(%esp) 
0x2487: addl $1, %ebx 
0x248a: shll $2, %ebx 
0x248d: addl %ecx, %ebx 
0x248f: movl %ebx, 8(%esp) 
0x2493: movl (%ebx), %eax 
0x2495: addl $4, %ebx 
0x2498: testl %eax, %eax 
0x249a: jne 0x00002493    ; start + 35 
0x249c: movl %ebx, 12(%esp) 
0x24a0: calll 0x000024b0    ; main at main.m:14 
**0x24a5: movl %eax, (%esp)** 
0x24a8: calll 0x000033ca    ; exit 
0x24ad: hlt  
0x24ae: nop  
0x24af: nop 

我把星號周圍的標記區域。無論是否存在異常斷點,都會顯示相同的錯誤。

我使用的故事板,這裏是.h文件的ViewController的:

#import <UIKit/UIKit.h> 

@interface ViewController : NSObject 
{ 
//outlets 
IBOutlet UITextField *billField; 

IBOutlet UITextField *tipFieldTen; 
IBOutlet UITextField *tipFieldFifteen; 
IBOutlet UITextField *tipFieldTwenty; 
IBOutlet UITextField *tipFieldCustom; 
IBOutlet UITextField *totalFieldTen; 
IBOutlet UITextField *totalFieldFifteen; 
IBOutlet UITextField *totalFieldTwenty; 
IBOutlet UITextField *totalFieldCustom; 
IBOutlet UILabel *customPercentLabel; 
IBOutlet UISlider *customPercentSlider; 

NSString *billTotal; 
} 
-(IBAction)calculateTip:(id)sender; 



@end 

這裏是.M文件:

#import "ViewController.h" 

@implementation ViewController 

- (void)awakeFromNib; 
{ 
[billField becomeFirstResponder]; //display keyboard for billField 
} 

-(IBAction)calculateTip:(id)sender 
{ 
static BOOL toggle = YES; 

if (toggle) 
{ 
    toggle = NO; 
    NSString *billFieldText = billField.text; 

    float newTotal = [billFieldText floatValue]; 
    float customTipPercent = customPercentSlider.value; 

    if(sender ==billField) 
    { 
     if (billFieldText.length <billTotal.length) 
      billTotal = [NSString stringWithFormat:@"%.02f", 
         newTotal/10]; 
     else 
      billTotal = [NSString stringWithFormat:@"%.02f", 
         newTotal * 10]; 

     billField.text = billTotal; 

     newTotal = [billTotal floatValue]; 

     float tenTip = newTotal * 0.10; 
     float fifteenTip = newTotal * 0.15; 
     float twentyTip = newTotal * 0.20; 

     tipFieldTen.text = [NSString stringWithFormat:@"%.02f", tenTip]; 
     tipFieldFifteen.text = [NSString stringWithFormat:@"%.02f", fifteenTip]; 
     tipFieldTwenty.text = [NSString stringWithFormat:@"%.02f", twentyTip]; 

     totalFieldTen.text = [NSString stringWithFormat:@"%.02f", newTotal + tenTip]; 
     totalFieldFifteen.text = [NSString stringWithFormat:@"%.02f", newTotal + fifteenTip]; 
     totalFieldTwenty.text = [NSString stringWithFormat:@"%.02f", newTotal + twentyTip]; 
    } 
    else if (sender == customPercentSlider) 
    { 
     int percentage = (int)(customTipPercent * 100); 
     customPercentLabel.text = [NSString stringWithFormat:@"%i%%", percentage]; 
     float newSliderValue = ((float) percentage)/100; 
     customPercentSlider.value = newSliderValue; 
     customTipPercent = newSliderValue; 
    } 
    float customTip = customTipPercent * newTotal; 
    tipFieldCustom.text = [NSString stringWithFormat:@"%.02f", customTip]; 
    totalFieldCustom.text = [NSString stringWithFormat:@"%.02f", customTip + newTotal]; 
} 
else 
{ 
    toggle = YES; 
} 

} 



@end 

任何幫助將不勝感激因爲我是objective-c

回答

3

你有

@interface ViewController : NSObject 

這也許應該是:

@interface ViewController : UIViewController 

原因:

所以你ViewController類是目前從NSObject,不執行initWithCoder繼承。實際上,此方法調用是NSCoding協議的一部分,其中UIViewController採用。

背景:

您收到基本上是說該類ViewController的對象被送到一個選擇,或者「消息」,initWithCoder,但它沒有這個方法的實現錯誤。因此,您通常需要做兩件事來追蹤:

1)檢查您是否在代碼中的任何地方發送該消息,然後檢查接收它的對象是否屬於應識別該方法的類。你還必須考慮從UIKit等框架中調用的方法,這使得這更加困難。 2)如果上面看起來不錯,那麼你不得不考慮,也許雖然你的代碼假設一個對象是一種類型,但它可能實際上是一個不同的類型,因此出現這種錯誤。

考慮以下代碼:

NSArray *anArray = [[NSArray alloc] initWithObjects:@"aString", nil]; 
NSNumber *aNumber = [anArray objectAtIndex:0]; 
BOOL isEqual = [aNumber isEqualToNumber:[NSNumber numberWithInteger:42]]; 

這將編譯沒有任何警告,但在運行時將導致異常 - 因爲它假設數組中的對象是NSNumber,但實際上它是NSString

*終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [__ NSCFConstantString isEqualToNumber:]:無法識別的選擇發送到實例0x6cbfc'

+0

非常感謝您的詳細回覆!我開始理解這個問題以及背後的理由感謝您的解釋 – Buzzd 2012-07-23 20:52:23

+0

不用擔心 - 有很多概念在學習Objective-C時必須學習。在這種情況下,閱讀協議! – ChrisH 2012-07-23 21:04:24