NSTextField - *textField 中的操作
NSTextField - actions within *textField
我需要帮助使用 NSTextField 及其 inputs/outputs。
在功能代码中,我有三个不同的 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 中做同样的事情时,我不知道该怎么做...我的想法是,该过程应该如下:
- 设置第一个整数(输入)
- *选择数学函数“/,,-,+”(method/action)
- 设置第二个整数(输入)
- 根据数学函数计算上述输入的结果(method/action)
但这实际上是我能够解释的全部...问题是我不知道如何我可以存储第一个输入值(在选择数学函数之前)和如何到计算第一个和第二个输入值之间的结果。
提前感谢您的每一个提示/link/参考/教程/等
只要你只有一个周期,就很容易了。 (顺便说一句:人类数学语法(中缀符号)对于计算机编程来说有点奇怪,需要优先级规则和括号,所以有人发明了另一种代数语法,PN or reverse PN。)
当按下操作符时,您将操作符和第一个操作数存储到 ivar 中。这是您进入的状态,通常在 GUI 编程中您试图阻止状态。然而,用户对计算器的期望是什么。
首先让我们使用属性和生成的 ivar 美化您的代码。
@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
接下来您需要为 "operation begin" 添加一个操作。您可以将它连接到文本字段并使其成为 "enter" 操作。
@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 中输入。
在我研究了你的代码之后......我终于得到了功能代码;-)
起初...这是应用程序的外观:
这是代码:
---------- 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
我认为效果很好....现在是时候添加下一个功能、带数字的按钮等...
感谢您展示正确的方法!
我需要帮助使用 NSTextField 及其 inputs/outputs。
在功能代码中,我有三个不同的 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 中做同样的事情时,我不知道该怎么做...我的想法是,该过程应该如下:
- 设置第一个整数(输入)
- *选择数学函数“/,,-,+”(method/action)
- 设置第二个整数(输入)
- 根据数学函数计算上述输入的结果(method/action)
但这实际上是我能够解释的全部...问题是我不知道如何我可以存储第一个输入值(在选择数学函数之前)和如何到计算第一个和第二个输入值之间的结果。
提前感谢您的每一个提示/link/参考/教程/等
只要你只有一个周期,就很容易了。 (顺便说一句:人类数学语法(中缀符号)对于计算机编程来说有点奇怪,需要优先级规则和括号,所以有人发明了另一种代数语法,PN or reverse PN。)
当按下操作符时,您将操作符和第一个操作数存储到 ivar 中。这是您进入的状态,通常在 GUI 编程中您试图阻止状态。然而,用户对计算器的期望是什么。
首先让我们使用属性和生成的 ivar 美化您的代码。
@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
接下来您需要为 "operation begin" 添加一个操作。您可以将它连接到文本字段并使其成为 "enter" 操作。
@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 中输入。
在我研究了你的代码之后......我终于得到了功能代码;-)
起初...这是应用程序的外观:
这是代码:
---------- 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
我认为效果很好....现在是时候添加下一个功能、带数字的按钮等...
感谢您展示正确的方法!