绑定不会更新NSTextField

时间:2017-02-13 14:14:53

标签: objective-c cocoa cocoa-bindings kvc

我在XIB中使用绑定到NSObjectController。当我设置NSObjectController的新内容对象时,唯一没有改变的文本字段值是具有第一响应者的值。模型更改没有问题。

如果我不使用自定义getter / setter,则firstResponder(isBeingEdited)的文本字段会发生变化而不会出现问题。

我的KVC,KVO出了什么问题?

我的自定义getter / setter低于pic。 PS:在改变内容对象以使其工作之前,我不想让窗口成为​​第一响应者。

enter image description here

static const CGFloat MMsInOneInch = 25.4;
static const CGFloat inchesInOneMM = 0.0393700787402;
- (void)setPaperWidth:(CGFloat)paperWidth
{
    [self willChange];
    CGFloat newWidth = paperWidth * [self conversionKoeficientToDefaultUnitType];
    if (!_isChangingPaperSize) {
        if (self.paperSize == PPPaperSizeA4 && fabs(newWidth - widthSizeOfA4) > 0.001) {
            [self setPaperSize:PPPaperSizeCustom];
        }
        if (self.paperSize == PPPaperSizeUSLetter && fabs(newWidth - widthSizeOfUSLetter) > 0.001 ) {
            [self setPaperSize:PPPaperSizeCustom];
        }
    }
    [self willChangeValueForKey:@"paperWidth"];
    _paperWidth = newWidth;
    [self didChangeValueForKey:@"paperWidth"];
    [self didChange];
}


- (CGFloat)conversionKoeficientToDefaultUnitType
{
    if ([self defaultUnitType]  == [self unitType]) {
        return 1;
    }
    if ([self defaultUnitType] == PPPrintSettingsUnitTypeMM) {
        return MMsInOneInch;
    }
    if ([self defaultUnitType] == PPPrintSettingsUnitTypeInch) {
        return inchesInOneMM;
    }
    return 1;
}

- (CGFloat)paperWidth
{
    return _paperWidth / [self conversionKoeficientToDefaultUnitType];
}

1 个答案:

答案 0 :(得分:0)

我忘了我使用带有最小/最大值的NSNumberFormatter来阻止NSTextField更新。

enter image description here