NSTextField持续更新 [英] NSTextField continuous update

查看:121
本文介绍了NSTextField持续更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何获取NSTextfield自动更新,而不必按返回"或单击另一个文本字段.

I can't figure out how to get an NSTextfield to update automatically, without having to press "Return" or click another text field.

我的目标是在一个字段中输入数字,并让其他字段同时更新.我尝试在文本字段属性中单击连续",但似乎没有任何作用.

My goal is to input a number into a field and have the other fields update simultaneously. I tried clicking "Continuous" in the text field attributes but it doesn't seem to do anything.

这是我的界面文件:

#import <Foundation/Foundation.h>

@interface InchController : NSObject {
    IBOutlet NSTextField *centimetersTextField;
    IBOutlet NSTextField *inchesTextField;
    IBOutlet NSTextField *feetTextField;
}

-(IBAction)convert:(id)sender;

@end

这是我的实现文件:

#import "InchController.h"

@implementation InchController

- (IBAction)convert:(id)sender {

    if (sender == inchesTextField) {
        float inches = [inchesTextField floatValue];
        [feetTextField setFloatValue:(inches * 0.0833)];
        [centimetersTextField setFloatValue:(inches * 2.54)];
    }
    else if (sender == feetTextField) {
        float feet = [feetTextField floatValue];
        [inchesTextField setFloatValue:(feet * 12)];
        [centimetersTextField setFloatValue:(feet * 30.48)];
    }
    else if (sender == centimetersTextField) {
        float centimeters = [centimetersTextField floatValue];
        [inchesTextField setFloatValue:(centimeters * 0.394)];
        [feetTextField setFloatValue:(centimeters * 0.033)];
    }

}

@end

因此,这是每个Josh解决方案的更新的实现文件.注释掉了IBAction,因为在实现和接口文件中不再需要它.

So here is the updated implementation file per Josh's solution. Commented out the IBAction since it is no longer needed in the implementation and interface files.

#import "LengthController.h"

@implementation LengthController

//- (IBAction) convert: (id)sender {
//}

-(void) controlTextDidChange:(NSNotification *) note {

    NSTextField *changedField = [note object];

    if (changedField == inchesTextField) {
        float inches = [inchesTextField floatValue];
        [feetTextField setFloatValue: (inches * 0.0833)];
        [centimetersTextField setFloatValue: (inches * 2.54)];
    }

    if (changedField == centimetersTextField) {
        float centimeters = [centimetersTextField floatValue];
        [inchesTextField setFloatValue:(centimeters * 0.394)];
        [feetTextField setFloatValue:(centimeters * 0.033)];
    }

    if (changedField == feetTextField) {
        float feet = [feetTextField floatValue];
        [inchesTextField setFloatValue:(feet * 12)];
        [centimetersTextField setFloatValue:(feet * 30.48)];
    }
}

@end

推荐答案

使您的控制器成为

Make your controller the delegate of the text fields; you can set this in Interface Builder by Ctrl-dragging from the text fields to the controller.

在您的控制器中,实现NSControl委托"方法

In your controller, implement the "NSControl Delegate" method controlTextDidChange:, which will be called (as its name suggests) whenever the field's text changes. In that method, you can validate the text and, if appropriate, update the contents of the other fields.

传入的参数可以为您提供已更改的文本字段;然后可以将其传递给现有的convert:方法以重用代码:

The argument that is passed in can give you the text field which changed; you can then pass that on to your existing convert: method to reuse the code:

- (void) controlTextDidChange: (NSNotification *)note {

    NSTextField * changedField = [note object];
    [self convert:changedField];
}

关于动作方法没有什么特别的. IBAction返回类型的计算结果为void; Xcode仅使用它公开用于Interface Builder的方法.因此,您可以像调用其他任何方法一样调用它们.在这里,您将获得适当的字段并将其作为sender参数传递,就好像该字段已经调用了操作方法本身一样.

There's nothing special about action methods. The IBAction return type evaluates to void; it's only used by Xcode to expose the method for use in Interface Builder. You can, therefore, call them just like any other method. Here, you get the appropriate field and pass it in as the sender parameter, as if the field had called the action method itself.

这篇关于NSTextField持续更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆