Objective c NSComboBox和NSTextView:在组合框值更改时将textview值保存到模型
虽然我在开发iOS应用程序方面很有经验,但在Mac OS X应用程序开发方面我还是有点新手 我遇到的问题如下 我已经创建了一个带有组合框、几个文本字段和两个文本视图的UI。每当用户更改NSCOMBOX的选定值时,我都希望将当前显示的所有值保护到我的模型中。在我当前的实现中,我使用nsComboxDelegate的委托方法将数据保存到模型中-ComboxWillPopup:-另一个委托方法用于加载数据-ComboxSelectionDidChange:。对于我的文本字段,所有操作都按预期进行,但我在将文本视图的数据保存到模型时遇到问题,我无法找出原因 关于NSTextView,我有以下问题:与在模型中保存旧本地化的值并加载新选择的本地化的值不同,似乎有一点缓存在进行中-旧本地化的值在textview中为新选择的本地化复制这也发生在我的数据模型中 视图控制器: 型号:Objective c NSComboBox和NSTextView:在组合框值更改时将textview值保存到模型,objective-c,macos,nstextview,nscombobox,Objective C,Macos,Nstextview,Nscombobox,虽然我在开发iOS应用程序方面很有经验,但在Mac OS X应用程序开发方面我还是有点新手 我遇到的问题如下 我已经创建了一个带有组合框、几个文本字段和两个文本视图的UI。每当用户更改NSCOMBOX的选定值时,我都希望将当前显示的所有值保护到我的模型中。在我当前的实现中,我使用nsComboxDelegate的委托方法将数据保存到模型中-ComboxWillPopup:-另一个委托方法用于加载数据-ComboxSelectionDidChange:。对于我的文本字段,所有操作都按预期进行,但我
在这两行有问题的代码中,您是否看到在text=textView.string;`线如果是这样,那么问题就出在模型的setValue方法中。另外,当text=textView.string时,selectedLocale设置为什么;调试器中的命中率?是的,值被分配给变量,但似乎存在某种奇怪的问题。不只是更改上次选择的本地化的值,通常还会更改新选择的本地化的值。我会进一步研究我的模型。亲爱的,你通过让我看模型帮助我解决了这个问题。我的属性使用了strong而不是copy,这导致了问题。祝你好运!
- (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;
}