1

虽然我在开发 iOS 应用程序方面经验丰富,但在 Mac OS X 应用程序开发方面我还是个新手。

我遇到的问题如下...

我创建了一个带有组合框、几个文本字段和 2 个文本视图的 UI。每当用户更改 NSComboBox 的选定值时,我都想将所有当前显示的值保存到我的模型中。在我当前的实现中,我使用 NSComboBoxDelegate 的委托方法将数据保存到模型中(-comboBoxWillPopUp:) - 另一个委托方法用于加载数据(-comboBoxSelectionDidChange:)。对于我的文本字段,一切都按预期工作,但我无法将文本视图的数据保存到模型中,而且我无法弄清楚是什么原因。

关于 NSTextView 我有以下问题:不仅仅是在模型中保存旧本地化的值并以某种方式加载新选择的本地化的值,似乎还有一些缓存正在进行 - 旧的值本地化被复制到文本视图中新选择的本地化,这也发生在我的数据模型中。

视图控制器:

- (void)comboBoxSelectionDidChange:(NSNotification *)notification
{    
    NSInteger index = [comboBox indexOfSelectedItem];
    NSString *selectedLocale = [supportedLocales objectAtIndex:index];

    text = [deal valueForContentItemWithType:SDDealContentItemTypeTitle locale:selectedLocale];
    titleField.stringValue = text ? text : @"";

    text = [deal valueForContentItemWithType:SDDealContentItemTypeText locale:selectedLocale];
    textView.string = text ? text : @"";
}

- (void)comboBoxWillPopUp:(NSNotification *)notification
{
    NSInteger index = [comboBox indexOfSelectedItem];
    NSString *selectedLocale = (index != NSUIntegerMax) ? [supportedLocales objectAtIndex:index] : [supportedLocales objectAtIndex:0];

// works fine ...

    text = titleField.stringValue;
    [deal setValue:text forContentItemWithType:SDDealContentItemTypeTitle locale:selectedLocale]; 

// has issues ...

    text = textView.string;
    [deal setValue:text forContentItemWithType:SDDealContentItemTypeText locale:selectedLocale]; 
}

模型:

- (id)valueForContentItemWithType:(SDDealContentItemType)type locale:(NSString *)locale
{
    SDDealContentItem *item = [self contentItemWithType:type locale:locale];

    return item ? item.value : nil;
}

- (void)setValue:(id)value forContentItemWithType:(SDDealContentItemType)type locale:(NSString *)locale
{    
    SDDealContentItem *item = [self contentItemWithType:type locale:locale];
    if (!item)
    {
        SDDealContentItem *item = [SDDealContentItem contentItemWithType:type locale:locale];
        item.value = value;

        self.items = [self.items arrayByAddingObject:item];

        return;
    }

    item.value = value;
}
4

0 回答 0