cocoanssegmentedcontrol

Saving title of NSSegmentedControl in User defaults


Thanks for the help:

I manually set the title of a segController segment from a textField input like this:

NSString  *labelString = [textField stringValue];
(textField.stringValue = labelString);
[segControl setLabel: labelString forSegment:8];

I loose the new label when quitting. How can I save the edited segController label string in NSUserDefaults as I would with a text string, like this:

    [[NSUserDefaults standardUserDefaults] setObject: [textField objectValue] forKey: @"newDefault"];

My action needs to occasionally set a new title. Point is the label string is not permanently fixed.

thanks.

Paul.


Solution

  • Assuming you know the segment number, You can do the following:

    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    if(defaults) {
        [defaults setValue: [segControl labelForSegment:8] forKey: @"segmentLabel"];
    }
    else {
        // handle error
    }
    

    Alternatively you can just save the string to NSUserDefaults whenever you set the label like in your above example.