2013-02-27 41 views
0

所以我正在计算器应用程序和我的代码目前不正确执行。我知道什么是错,但我不知道如何去解决它。目前,我有我的当前代码ViewController.m当我按下添加按钮:我没有正确执行我的push/pop方法,有人可以帮我吗?

#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包装。

回答

1

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

logic = [[CalcLogic alloc] init]; 
+0

既然被定义为公共财产,也许它是由外部类分配。否则就不需要公共财产。 – rmaddy 2013-02-27 18:23:57

+0

@rmaddy公共财产有很多用途。就目前而言,他的代码没有证明对象正在被初始化,所以这是我第一个假设它为什么不起作用,如果你在代码中使用self.logic,那么没有'CalcLogic'类的进一步实现细节 – 2013-02-27 18:24:54

+0

然后初始化它与自己像 self.logic = [[CalcLogic alloc] init]; – razibdeb 2013-02-27 18:27:39

相关问题