2

这是底部的“双重结果”和“如果”出现的错误。

不知道为什么会发生这种情况,它来自 Itunes U 讲座,据我多次扫描它可以看出它是完全相同的。

问题

没有可见的@interface 'CalculatorViewController' 声明选择器 'enterPressed'

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

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



@implementation CalculatorViewController

@synthesize display = _display;

@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;

@synthesize brain = _brain;

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

- (IBAction)digitPressed:(UIButton *)sender 
{
    NSString *digit = sender.currentTitle;

    if(self.userIsInTheMiddleOfEnteringANumber){

    self.display.text = [self.display.text stringByAppendingString:digit];

    } else {

        self.display.text = digit;

        self.userIsInTheMiddleOfEnteringANumber = YES;

    }
}

- (IBAction)enterPressed:(id)sender {
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;
}

- (IBAction)operationPressed:(UIButton *)sender {
    if(self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
    double result = [self.brain performOperand:sender.currentTitle];
    NSString *resultString = [NSString stringWithFormat:@"%g", result];
    self.display.text = resultString;
}

@end
4

1 回答 1

2

你有:

if(self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];

将其更改为:

if(self.userIsInTheMiddleOfEnteringANumber) [self enterPressed:self];

您收到错误是因为您忘记了在(id)sender中声明的参数enterPressed

- (IBAction)enterPressed:(id)sender
于 2013-05-20T01:13:34.630 回答