2013-05-17 89 views
0

我在xcode 4.5中構建了這個計算器應用程序,數字按預期工作,但操作即(+, - ,c)曾經工作過一次,因爲它們只返回Nan和inf,並且輸入按鈕總是使應用程序崩潰並返回錯誤below.I建相關的方法SIGARBT錯誤崩潰應用程序

@autoreleasepool { 
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([CalculatorAppDelegate  
class])); 

在故事板的UI和鏈接按鈕,這是我的CalculatorViewController.h

#import <UIKit/UIKit.h> 

@interface CalculatorViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *display; 

@end 

這是我CalculatorViewController.m

#import "CalculatorViewController.h" 
#import "CalculatorBrain.h" 

@interface CalculatorViewController() 
@property(nonatomic) BOOL userIsInTheMidleOfEnteringANumber; 
@property (nonatomic, strong)CalculatorBrain *brain; 
@end 

@implementation CalculatorViewController 

@synthesize display = _display; 
@synthesize userIsInTheMidleOfEnteringANumber =_userIsInTheMidleOfEnteringANumber; 
@synthesize brain = _brain; 

- (CalculatorBrain *)brain 
{ 
    if (!_brain)_brain=[[CalculatorBrain alloc]init]; 
return _brain; 
} 

- (IBAction)digitPressed:(UIButton *)sender 
{ 
    NSString *digit = sender.currentTitle; 
    if (self.userIsInTheMidleOfEnteringANumber){ 
    self.display.text = [self.display.text stringByAppendingString:digit]; 
} 
    else { 
     self.display.text = digit; 
     self.userIsInTheMidleOfEnteringANumber=YES; 
    } 
} 
- (IBAction)enterPressed 
{ 
    NSLog(@"im hre"); 
    [self.brain pushOperand:[self.display.text doubleValue]]; 
    self.userIsInTheMidleOfEnteringANumber=NO; 
} 
- (IBAction)operationPresed:(UIButton *)sender 
{ 
    if (self.userIsInTheMidleOfEnteringANumber) [self enterPressed]; 
    double result =[self.brain performOperation:sender.currentTitle]; 
    NSString *resultString= [NSString stringWithFormat:@"%g",result]; 
    self.display.text= resultString; 
} 
    @end 

這是我CalculatorBrain.h

#import <Foundation/Foundation.h> 

@interface CalculatorBrain : NSObject 
-(void)pushOperand:(double)operand; 
-(double)performOperation:(NSString *)operation; 

@end 

這是我CalculatorBrain.m

#import "CalculatorBrain.h" 
@interface CalculatorBrain() 
@property (nonatomic,strong)NSMutableArray *operandStack; 
@end 

@implementation CalculatorBrain 
@synthesize operandStack = _operandStack; 

-(NSMutableArray *)operandStack 
{ 
    if (_operandStack== nil) _operandStack =[[NSMutableArray alloc] init]; 
    return _operandStack; 
} 

-(void)pushOperand:(double)operand 
{ 

    [self.operandStack addObject:[NSNumber numberWithDouble:operand]]; 
} 
-(double)popOperand 
{ 
    NSNumber *operandObject = [self.operandStack lastObject]; 
    if (operandObject) [self.operandStack removeLastObject]; 
    return [operandObject doubleValue]; 
} 

-(double)performOperation:(NSString *)operation 
{ 
    double result=0; 
    //calculate result 
    if ([operation isEqualToString:@"+"]) 
    { 
     result =[self popOperand]+ [self popOperand]; 
    } 
    else if ([operation isEqualToString:@"*"]) 
    { 
    result = [self popOperand] * [self popOperand]; 
    } 
    else if ([operation isEqualToString:@"-"]) { 
     result = [self popOperand] - [self popOperand]; 
    } 
    else if ([operation isEqualToString:@"/"]) { 
     result = [self popOperand]/[self popOperand]; 
    } 
    [self pushOperand:result]; 

    return result; 
} 

@end

我可是從一個例子工作,並且已經按照我不明白的所有步驟什麼即時做錯了enterPressed方法沒有類型,即UIButton這是我的控制檯輸出

2013-05-17 15:05:15.221 Calculator[786:11303] im hre 
2013-05-17 15:05:15.280 Calculator[786:11303] -[CalculatorViewController enterPressed:]: unrecognized selector sent to instance 0x922a620 
2013-05-17 15:05:15.288 Calculator[786:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CalculatorViewController enterPressed:]: unrecognized selector sent to instance 0x922a620' 
*** First throw call stack: 
(0x1c8e012 0x10cbe7e 0x1d194bd 0x1c7dbbc 0x1c7d94e 0x10df705 0x16920 0x168b8 0xd7671 0xd7bcf 0xd6d38 0x4633f 0x46552 0x243aa 0x15cf8 0x1be9df9 0x1be9ad0 0x1c03bf5 0x1c03962 0x1c34bb6 0x1c33f44 0x1c33e1b 0x1be87e3 0x1be8668 0x1365c 0x1f0d 0x1e35) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

任何幫助將b讚賞

回答

1

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CalculatorViewController enterPressed:]: unrecognized selector sent to instance 0x922a620'是這裏的答案。檢查您的CalculatorViewController課程中是否有方法enterPressed:。我敢肯定,它以某種方式拼寫錯誤,如果不缺

1

更改方法簽名:

- (IBAction)enterPressed 

- (IBAction)enterPressed:(id)sender 
0

在你CalculatorViewController.m文件替換

- (IBAction)enterPressed 

t

- (IBAction)enterPressed:(UIButton *)sender 
0

我有這個確切的相同的問題,實際上...我刪除了輸入按鈕,並重新創建它,然後它的工作。祝你好運,我想把我的頭髮拉出來

0

下面有很多工作答案。你遇到這個問題的原因是,你在xib文件中鏈接到你的一個按鈕的方法出現在xib上,指向enterPressed: - 一個帶參數的方法(enterPressed後跟一個冒號),但是你有編碼一個沒有參數的方法(enterPressed),因此導致崩潰。代碼試圖找到與你在你的XIB文件已鏈接參數的方法,但它並沒有找到它,會導致崩潰說 - 「無法識別的選擇發送到實例」

因此只需更換

- (IBAction)enterPressed 

與此:

- (IBAction)enterPressed:(UIButton *)sender