本文介绍了将NSTextField绑定到NSNumber的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用NSTextField进行整数用户输入。文本字段绑定到NSNumber属性,在setter方法中,我清理输入值(确保它是一个int)并设置属性如果必要。我发送willChangeValueForKey:和didChangeValueForKey :,但是当文本字段仍然活动时,UI不会更新为新的值。

I'm trying to use an NSTextField for integer user input. The text field is bound to an NSNumber property and in the setter method I cleanup the input value (make sure it's an int) and set the property if necessary. I send the willChangeValueForKey: and didChangeValueForKey:, but the UI doesn't update to the new values while that text field is still active.

例如,我可以在文本字段中输入12abc,setter方法清除为12,但文本字段仍显示12abc。

So for example I can type "12abc" in the text field that the setter method cleans up to "12", but the text field still shows "12abc".

我在界面生成器中选中了Continuously Update Value。

I have "Continuously Update Value" checked in the interface builder.

(我也注意到setter方法接收到NSString,而不是NSNumber,是否正常?)

(I've also noticed that the setter method receives an NSString, not an NSNumber. Is that normal?)

将NSTextField连接到NSNumber的正确方法是什么?该属性的setter方法是什么样子的?如何防止非数值显示在文本字段中?

What's the correct way of hooking up an NSTextField to an NSNumber? What does the setter method look like for the property? How to prevent non-numeric values from showing up in the text field?

推荐答案

很少的理由发送这些消息。通常,通过实现和使用访问器(或者更好的属性),你可以做更好,更干净的工作。

There are very few reasons to send those messages. Usually, you can do the same job better and more cleanly by implementing and using accessors (or, better yet, properties). KVO will send the notifications for you when you do that.

在您的情况下,您要拒绝或过滤虚假输入(例如12abc)。

In your case, you want to either reject or filter bogus inputs (like "12abc"). The correct tool for this task is Key-Value Validation.

要启用此功能,请检查IB中绑定上的立即验证框,并实施验证方法。

To enable this, check the "Validates Immediately" box on the binding in IB, and implement a validation method.

筛选:

- (BOOL) validateMyValue:(inout NSString **)newValue error:(out NSError **)outError {
    NSString *salvagedNumericPart;
    //Determine whether you can salvage a numeric part from the string; in your example, that would be "12", chopping off the "abc".
    *newValue = salvagedNumericPart; //@"12"
    return (salvagedNumericPart != nil);
}

拒绝:

- (BOOL) validateMyValue:(inout NSString **)newValue error:(out NSError **)outError {
    BOOL isEntirelyNumeric;
    //Determine whether the whole string (perhaps after stripping whitespace) is a number. If not, reject it outright.
    if (isEntirelyNumeric) {
        //The input was @"12", or it was @" 12 " or something and you stripped the whitespace from it, so *newValue is @"12".
        return YES;
    } else {
        if (outError) {
            *outError = [NSError errorWithDomain:NSCocoaErrorDomain code: NSKeyValueValidationError userInfo:nil];
        }
        //Note: No need to set *newValue here.
        return NO;
    }
}



将字符串转换为数字,将一个数字格式化程序连接到 formatter 插件,或者在您的验证方法中将NSNumber替换为NSString。

Yes, unless you use a value transformer that transforms strings into numbers, connect a number formatter to the formatter outlet, or substitute an NSNumber for the NSString in your validation method.

这篇关于将NSTextField绑定到NSNumber的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:30