0

我在 xcode 4.5 中构建了这个计算器应用程序,数字按预期工作,但操作 ie (+,- ,c) 工作了一次并且从未工作过,因为它们只返回 Nan 和 inf 并且输入按钮总是使应用程序崩溃并返回下面的错误。我在情节提要中构建 UI,并将按钮链接到相关方法

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

这是我的 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;
}

@结尾

我从一个示例开始工作并遵循所有步骤我不明白我做错了什么 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) 

任何帮助将不胜感激

4

5 回答 5

1

更改方法签名:

- (IBAction)enterPressed

- (IBAction)enterPressed:(id)sender
于 2013-05-17T14:17:51.413 回答
1

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CalculatorViewController enterPressed:]: unrecognized selector sent to instance 0x922a620'是这里的答案。检查您enterPressed:CalculatorViewController课程中是否有该方法。我敢肯定,如果没有丢失,它会以某种方式拼写错误

于 2013-05-17T14:17:15.773 回答
0

在您的CalculatorViewController.m文件中替换

- (IBAction)enterPressed

- (IBAction)enterPressed:(UIButton *)sender
于 2013-05-17T14:29:33.423 回答
0

我实际上遇到了同样的问题......我删除了 Enter 按钮并重新创建了它,然后它就可以工作了。祝你好运,我想把头发拉出来

于 2013-08-11T16:43:59.210 回答
0

下面有很多可行的答案。您遇到此问题的原因是,您在 xib 文件中针对您的一个按钮链接的方法出现在 xib 上,指向enterPressed:- 带有参数的方法(enterPressed 后跟冒号),但您已经编码没有参数的方法(enterPressed),因此会导致崩溃。代码试图找到一个带有您在 xib 文件中链接的参数的方法,但它没有找到它,导致崩溃说 - '无法识别的选择器发送到实例'

因此只需更换

- (IBAction)enterPressed

有了这个:

- (IBAction)enterPressed:(UIButton *)sender
于 2013-08-11T17:54:10.330 回答