动态更新 NSTextField 中的数字
我在制作动态单位转换器时遇到问题。我有两个文本字段:一个用于“英寸”,另一个用于“英尺”。我的目标是在输入数字时将英寸转换为英尺,反之亦然。我希望该操作动态发生,而不是在按下“Enter”或“Tab”按钮时发生。
我正在将 NSTextField 与数字格式化程序一起使用。所以我需要格式化程序在计算时也能工作。我尝试过 -(void)controlTextDidChange 方法,但格式化程序无法使用它。
请参阅下面的我当前的代码,该代码不能动态工作。
#import <Foundation/Foundation.h>
@interface Convert : NSObject {
IBOutlet NSTextField *inches;
IBOutlet NSTextField *feet;
}
- (IBAction)textDidChange:(id)sender;
@end
谁能
#import "Convert.h"
@implementation Convert
- (IBAction)textDidChange:(id)sender {
if (sender == inches) {
float myInches = [inches floatValue];
[feet setFloatValue: (myInches/12)];
}
else if (sender == feet) {
float myFeet = [feet floatValue];
[inches setFloatValue: (myFeet*12)];
}
}
@end
告诉我如何在不按“Enter”或“Tab”键的情况下完成此操作?
...现在尝试使用 NSForm 而不是 NSTextField 的 NSValueTransformer 方法...
#import <Foundation/Foundation.h>
@interface LengthConverter : NSValueTransformer
+ (Class)transformedValueClass;
+ (BOOL)allowsReverseTransformation;
- (id)transformedValue:(id)value;
- (id)reverseTransformedValue:(id)value;
@end
并且
#import "LengthConverter.h"
@implementation LengthConverter
+ (Class)transformedValueClass {
return [NSNumber class];
}
+ (BOOL)allowsReverseTransformation {
return YES;
}
- (id)transformedValue:(id)value {
if (value == nil) return nil;
if (![value respondsToSelector: @selector(doubleValue)]) {
[NSException raise:NSInternalInconsistencyException format:@"Value does not respond",[value class]];
}
CGFloat inchesInputValue = [value doubleValue]; // handles NSString and NSNumber
CGFloat feetOutputValue = inchesInputValue / 12;
return [NSNumber numberWithDouble: feetOutputValue];
}
- (id)reverseTransformedValue:(id)value {
if (value == nil) return nil;
if (![value respondsToSelector: @selector(doubleValue)]) {
[NSException raise:NSInternalInconsistencyException format:@"Value does not respond",[value class]];
}
CGFloat feetInputValue = [value doubleValue]; // handles NSString and NSNumber
CGFloat inchesOutputValue = feetInputValue * 12;
return [NSNumber numberWithDouble: inchesOutputValue];
}
@end
I am having trouble making a dynamic unit converter. I have two text fields: one for "inches" and another for "feet". My goal is to convert the inches to feet as the numbers are being entered and vice versa. I want the action to happen dynamically, not when the "Enter" or "Tab" button is pressed.
I am using the NSTextField with Number Formatter. So I need the formatter to also work as the calculation takes place. I have tried the -(void)controlTextDidChange method but the formatter does not work with it.
See below for my current code which does not work dynamically.
#import <Foundation/Foundation.h>
@interface Convert : NSObject {
IBOutlet NSTextField *inches;
IBOutlet NSTextField *feet;
}
- (IBAction)textDidChange:(id)sender;
@end
and
#import "Convert.h"
@implementation Convert
- (IBAction)textDidChange:(id)sender {
if (sender == inches) {
float myInches = [inches floatValue];
[feet setFloatValue: (myInches/12)];
}
else if (sender == feet) {
float myFeet = [feet floatValue];
[inches setFloatValue: (myFeet*12)];
}
}
@end
Can anyone tell me how to accomplish this without having to press the "Enter" or "Tab" key?
...Now trying the NSValueTransformer approach using NSForm instead of NSTextField...
#import <Foundation/Foundation.h>
@interface LengthConverter : NSValueTransformer
+ (Class)transformedValueClass;
+ (BOOL)allowsReverseTransformation;
- (id)transformedValue:(id)value;
- (id)reverseTransformedValue:(id)value;
@end
and
#import "LengthConverter.h"
@implementation LengthConverter
+ (Class)transformedValueClass {
return [NSNumber class];
}
+ (BOOL)allowsReverseTransformation {
return YES;
}
- (id)transformedValue:(id)value {
if (value == nil) return nil;
if (![value respondsToSelector: @selector(doubleValue)]) {
[NSException raise:NSInternalInconsistencyException format:@"Value does not respond",[value class]];
}
CGFloat inchesInputValue = [value doubleValue]; // handles NSString and NSNumber
CGFloat feetOutputValue = inchesInputValue / 12;
return [NSNumber numberWithDouble: feetOutputValue];
}
- (id)reverseTransformedValue:(id)value {
if (value == nil) return nil;
if (![value respondsToSelector: @selector(doubleValue)]) {
[NSException raise:NSInternalInconsistencyException format:@"Value does not respond",[value class]];
}
CGFloat feetInputValue = [value doubleValue]; // handles NSString and NSNumber
CGFloat inchesOutputValue = feetInputValue * 12;
return [NSNumber numberWithDouble: inchesOutputValue];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 NSValueTransformer 绑定值。
NSValueTransformer
提供对象到对象的转换,您也可以在 XIB 和绑定中使用它。系统定义了一些,您可以很容易地定义自己的。 Apple 的TemperatureConverter 示例是介绍NSValueTransformer
的一个很好的起点。根据示例进行更新
在
+ [ApplicationDelegate初始化]
中,您将看到程序注册了它需要的所有转换器。他们注册一个名称,例如@“centrigradeFromKelvin”
。一旦您需要的 trafos 注册完毕,您就可以在 NIB 中使用转换器。在TemperatureConvertoer 示例中,您可以:
You can bind values using
NSValueTransformer
s.NSValueTransformer
offers object to object transformations, which you can also use in XIBs and bindings. The system defines a few, and you can define your own quite easily. Apple's TemperatureConverter sample would be a good starting point for an introduction toNSValueTransformer
.Update in response to sample
In
+ [ApplicationDelegate initialize]
, you'll see the program register all the transformers it needs. They register to a name, such as@"centrigradeFromKelvin"
.Once the trafos you will need are registered, you can use the transformer in the NIB. In the TemperatureConvertoer example, you can: