NSComboBox & NSTextView: save textview value to model on combo box value change

900 views Asked by At

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

There are 0 answers