NSComboBox& NSTextView:将textview值保存为组合框值更改的模型

时间:2012-01-13 13:12:47

标签: objective-c macos nstextview nscombobox

虽然我对开发iOS应用程序非常有经验,但对于Mac OS X应用程序开发,我仍然是一个新手。

我遇到的问题如下......

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

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

查看控制器:

- (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;
}

0 个答案:

没有答案