我正在制作一个简单的计算器应用程序,显示用户输入的等式以及等式的输出。在我的头文件的顶部,我创建了三个整数和一个数组...
#import <UIKit/UIKit.h>
int Method;
int SelectNumber;
float RunningTotal;
NSMutableArray *Equation;
@interface SecondCalculatorViewController : UIViewController{
IBOutlet UILabel *EquationLabel;
IBOutlet UILabel *ResultLabel;
}
- (IBAction)OneButton:(id)sender;
- (IBAction)TwoButton:(id)sender;
- (IBAction)ThreeButton:(id)sender;
- (IBAction)FourButton:(id)sender;
- (IBAction)FiveButton:(id)sender;
- (IBAction)SixButton:(id)sender;
- (IBAction)SevenButton:(id)sender;
- (IBAction)EightButton:(id)sender;
- (IBAction)NineButton:(id)sender;
- (IBAction)ZeroButton:(id)sender;
- (IBAction)Plusbutton:(id)sender;
- (IBAction)MinusButton:(id)sender;
- (IBAction)MultiplyButton:(id)sender;
- (IBAction)DivideButton:(id)sender;
- (IBAction)CalculateButton:(id)sender;
- (IBAction)ClearButton:(id)sender;
@end
在我的实现中,我准备将每个输入显示在标签中,然后我尝试将SelectNumber保存到我创建的数组的第一个索引中。然后我尝试在标签中显示它。以下是其中一个按钮的示例......
- (IBAction)OneButton:(id)sender {
//[Equation addObject:0];
SelectNumber = SelectNumber * 10;
SelectNumber = SelectNumber + 1;
//EquationLabel.text = [NSString stringWithFormat:@"%i",SelectNumber];
[Equation replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%i",SelectNumber]];
EquationLabel.text = [Equation objectAtIndex:0];
}
当我在模拟器中运行并按任意数字时,它不会显示数组中的第一个值&#34; Equation&#34;。
我该如何解决这个问题?
答案 0 :(得分:0)
似乎没有初始化方程式。你需要初始化它:
Equation = [NSMutableArray new];
// or Equation = [[NSMutableArray alloc] init];
在您的情况下, viewDidLoad
方法可能是此代码的好地方。