Search code examples
cocoanssegmentedcontrol

NSSegmentedControl Selected Segment in User Defaults


Having a problem storing the selectedSegment of segmented control in the User Defaults. I'm using the Segment Selection binding and doing this to set and call the default:

[[NSUserDefaults standardUserDefaults] setObject: [modeSegController objectValue] forKey: @"selTag"];

[modeSegController setObjectValue: [[NSUserDefaults standardUserDefaults] objectForKey: @"selTag"]];

Not working. Can someone help me out?

-thanks

Paul.


Solution

  • Use [[NSUserDefaults standardUserDefaults] setValue: [ NSNumber numberWithInteger:[modeSegController selectedSegment] ] forKey: @"selTag"]

    To show the selected segement use in the UI [modeSegController setSelected:...] the integer

    and

    [modeSegController setSelectedSegment:[ [NSUserDefaults standardUserDefaults] valueForKey: @"selTag"] integerValue] ]

    You needn't use tags to store/restore the selected segment. The segments can be accessed directly.