0

所以我正在开发一个计算器应用程序,我的代码目前没有正确执行。我知道出了什么问题,但我不确定如何解决它。目前,当我按下添加按钮时,我的 ViewController.m 带有当前代码:

#import "ViewController.h"
#import "CalcLogic.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *display;
@property (weak, nonatomic) IBOutlet UILabel *lastOperation;
@property (strong, nonatomic) CalcLogic* logic;

@end

@implementation ViewController
double result = 0;
//Last operation entered into the calculator
NSString* lastEntered;
@synthesize logic;

-(IBAction)numPressed:(UIButton *)sender{
    BOOL hasBeenCleared = [self.lastOperation.text isEqualToString:@"Clear"];

    if ([self.display.text isEqualToString:@"0."]) {
        self.display.text = sender.currentTitle;;
        self.lastOperation.text = sender.currentTitle;;
        [self.logic pushNumber:[sender.currentTitle doubleValue]];
    }
    else{
        self.display.text = [self.display.text stringByAppendingString:sender.currentTitle];
        if (self.lastOperation.text.length > 1 && hasBeenCleared != TRUE) {
            self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
        }
        else {
            self.lastOperation.text = sender.currentTitle;
        }
        [self.logic pushNumber:[sender.currentTitle doubleValue]];
    }
}

-(IBAction)clearPressed:(UIButton *)sender{
    self.display.text = @"0.";
    self.lastOperation.text = @"Clear";
    [self.logic clearStack];
    result = 0;
}

-(IBAction)operation:(UIButton *)sender{
    [logic pushOperation:sender.currentTitle];
    NSString* resultString = [NSString stringWithFormat:@"%g", result];
    self.display.text = resultString;
    if ([self.lastOperation.text isEqualToString:@"Clear"]) {
        self.lastOperation.text = @"";
        self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
    }
    else{
        self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
    }
}

-(IBAction)equalHit:(UIButton *)sender{
    result = [self.logic performOperation];
    self.display.text = [NSString stringWithFormat:@"%g", result];

}

我的问题是向数组推送和弹出对象。数组位于 中logic,我试图将数字推送到 中的两个数组之一logic,并将运算符推送到对象中的另一个数组。但是,我一定是做错了什么,因为当我签入控制台时没有推送任何内容(据我所知)。我对这种语言还是新手,并且来自 Java 包装。

4

1 回答 1

1

看起来您没有logic在代码中的任何地方分配/初始化。您需要这一行,可能在viewDidLoad或其他一些初始化函数中:

logic = [[CalcLogic alloc] init];
于 2013-02-27T18:22:30.283 回答