我需要使用NSTextField及其输入/输出的帮助。
在一个正常运行的代码中我有三个不同的NSTextFields(在Cocoa + Obj-C中)我知道如何从更多整数输入计算结果......
--- AppController.h ---
@interface AppController : NSObject {
IBOutlet NSTextField *firsTextField; // 1st Outlet
IBOutlet NSTextField *secondTextField; // 2nd Outlet
IBOutlet NSTextField *resultTextField; // 3rd Outlet
}
- (IBAction)result:(id)sender;
@end
--- AppController.m ---
@Implementation AppController
- (IBAction)result:(id)sender {
double first = [firstTextField doubleValue]; // set value 1st outlet
double second = [secondTextField doubleValue]; // set value 2nd outlet
double result = first + second; // count result
[resultTextField setDoubleValue:result]; // set value 3rd outlet
}
@end
但是当我尝试只在一个NSTextField中做同样的事情时,我不知道该怎么做......我的想法是,这个过程应该遵循:
但它实际上是我能解释的全部......问题是我不知道如何我可以存储第一个输入值(之前)选择数学函数)和如何到计算第一和第二输入值之间的结果。
提前感谢您提供的每个提示/链接/参考/教程/等等。
答案 0 :(得分:1)
只要你只有一个周期,就很容易了。 (顺便说一句:人类数学语法(中缀符号)对于计算机编程来说有点奇怪,需要优先级规则和括号,因此有些人发明了另一种代数语法PN或reverse PN。)
按下操作符时,将操作符和第一个操作数存储到ivar中。这是您输入的状态,通常在GUI编程中尝试阻止状态。但它是用户对计算器的期望。
首先让我们用属性和生成的ivars来美化你的代码。
@interface AppController : NSObject
@property (weak, nonatomic) IBOutlet NSTextField *operandTextField;
@property (weak, nonatomic) IBOutlet NSTextField *resultTextField;
@end
添加第一个值和操作的属性:
@interface AppController : NSObject
@property (weak, nonatomic) IBOutlet NSTextField *operandTextField;
@property (weak, nonatomic) IBOutlet NSTextField *resultTextField;
@property (nonatomic) double operand;
@property (nonatomic) NSString *operator;
@end
然后您需要操作的操作。以下是一个例子:
@interface AppController : NSObject
@property (weak, nonatomic) IBOutlet NSTextField *operandTextField;
@property (weak, nonatomic) IBOutlet NSTextField *resultTextField;
@property (nonatomic) double operand;
@property (nonatomic) NSString *operator;
- (IBAction)plus:(id)sender;
@end
接下来你需要为"操作开始"添加一个动作。您可以将其连接到文本字段并使其成为"输入"动作。
@interface AppController : NSObject
@property (weak, nonatomic) IBOutlet NSTextField *operandTextField;
@property (weak, nonatomic) IBOutlet NSTextField *resultTextField;
@property (nonatomic) double operand;
@property (nonatomic) NSString *operator;
- (IBAction)plus:(id)sender;
- (IBAction)operate:(id)sender;
@end
好的,完成了界面。让我们来实现:
@implementation AppController
- (IBAction)plus:(id)sender
{
// Store operator
self.operator = [valueTextField doubleValue]; // Some checking?
// Store operation
self.operation = @"+"; // You can use int constant for that, but this is the easy way
}
- (IBAction)operate:(id)sender
{
// Check, whether a operation is already selected
if ([self.operation length]==0)
{
// No: Do nothing
return;
}
// Get the second operand
double operand = [valueTextField doubleValue];
// Do the calculation
double result;
if ([self.operation isEqualToString:@"+"])
{
result = self.operand + operand;
}
else if ([self.operation isEqualToString:@"-"])
{
result = self.operand - operand;
}
…
[self.resultTextField setDoubleValue:result];
self.operation = @"";
// If you do not set this, the next operand will start a new calculation with the first
// operand. Another feature would be to store the result we got and repeatedly perform
// the operation on the last result. But I would change the UI in that case.
}
@end
输入Safari。
答案 1 :(得分:0)
在我研究你的代码之后......我终于得到了功能代码;-)
起初......这是看起来app的方式:
这是代码:
---------- CalcController.h -----------
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
@interface CalcController : NSObject {
IBOutlet NSTextField *textField;
}
@property (weak) IBOutlet NSTextField *operandTextField;
@property (weak) IBOutlet NSTextField *resultTextField;
@property (nonatomic) double value1;
@property (nonatomic) double value2;
@property (nonatomic) double result;
@property (nonatomic) NSString *operator;
- (IBAction)plus:(id)sender;
- (IBAction)result:(id)sender;
@end
---------- CalcController.m -----------
#import "CalcController.h"
@implementation CalcController
@synthesize operandTextField;
@synthesize resultTextField;
@synthesize value1;
@synthesize value2;
@synthesize result;
@synthesize operator;
- (IBAction)plus:(id)sender {
value1 = [textField doubleValue];
NSLog(@"operand1 = %lf", value1); // for test and see value in console
[textField setStringValue:@""]; // for clear text after 1st input done
operator = @"+";
}
- (IBAction)result:(id)sender {
value2 = [textField doubleValue];
NSLog(@"operand2 = %lf", value2); // for test and see value in console
if ([operator isEqualToString:@"+"]) {
result = value1 + value2;
NSLog(@"value1: %lf + value2: %lf = result: %lf", value1, value2, result); // for tests and see values in console
}
[resultTextField setDoubleValue:result];
operator = @"";
}
@end
我认为工作正常......现在是时候添加下一个功能,带数字的按钮等......
感谢您展示正确的方法!