NSComboBox 和 NSComboBox NSTextView:在组合框值更改时将文本视图值保存到模型

发布于 2024-12-27 00:03:29 字数 2297 浏览 2 评论 0原文

虽然我在开发 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;
}

While I'm quite experienced with developing iOS applications, I'm still a bit of a newbie with regards to Mac OS X application development.

The issue I'm having is the following ...

I've created a UI with a combo box, several text fields and 2 text views. Whenever the user changes the selected value of the NSComboBox I want to safe all currently displayed values to my model. In my current implementation I make use of a delegate method of NSComboBoxDelegate to save the data into the model (-comboBoxWillPopUp:) - another delegate method is used to load the data (-comboBoxSelectionDidChange:). All works as expected for my text fields, but I'm having trouble saving the data of the text views to the model and I can't figure out what's the cause.

With regards to the NSTextView I have the following issue: instead of just saving the values for the old localization in the model and loading the values of the newly selected localization somehow there seems to be a bit of caching going on - the values of the old localization are copied over for the newly selected localization in the textview and this happens in my data model as well.

view controller:

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

model:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文