动态更新 NSTextField 中的数字

发布于 2025-01-02 01:43:04 字数 2352 浏览 6 评论 0原文

我在制作动态单位转换器时遇到问题。我有两个文本字段:一个用于“英寸”,另一个用于“英尺”。我的目标是在输入数字时将英寸转换为英尺,反之亦然。我希望该操作动态发生,而不是在按下“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒儿 2025-01-09 01:43:04

您可以使用 NSValueTransformer 绑定值。

NSValueTransformer 提供对象到对象的转换,您也可以在 XIB 和绑定中使用它。系统定义了一些,您可以很容易地定义自己的。 Apple 的TemperatureConverter 示例是介绍NSValueTransformer 的一个很好的起点。

根据示例进行更新

+ [ApplicationDelegate初始化]中,您将看到程序注册了它需要的所有转换器。他们注册一个名称,例如@“centrigradeFromKelvin”

一旦您需要的 trafos 注册完毕,您就可以在 NIB 中使用转换器。在TemperatureConvertoer 示例中,您可以:

  1. 导航到表单单元格
  2. 选择表单单元格 - 摄氏度
  3. 按 cmd+opt+7 查看字段绑定
  4. 该值绑定到 SharedDefaults 中的值 - 值转换器设置为“centrigradeFromKelvin”。

You can bind values using NSValueTransformers.

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 to NSValueTransformer.

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:

  1. navigate to the form cells
  2. select Form Cell - Centigrade
  3. Press cmd+opt+7 to view the fields bindings
  4. The value is bound to a value in SharedDefaults - the value transformer is set as "centrigradeFromKelvin".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文