Search code examples
iphoneobjective-cnsuserdefaultsapplication-lifecycle

restoring segmenedControl's selected index after application being terminated


so i have this segmented control in the interface i am using user defaults to store the selected segment in applications view controller's applicationDidEnterBackground and restoring it in - (void)applicationWillEnterForeground {

NSNumber *indexNumber;

if (indexNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedIndex"]) {

NSInteger selectedIndex = [indexNumber integerValue]; self.segmentedControl.selectedSegmentIndex = selectedIndex; }

but when im launching the app, selecting a segment and then killing the application, i get this error. Program received signal: “SIGKILL” on relaunch.

any help would be appreciated


Solution

  • Hard to tell without more info, but likely this is caused because the controller and segmented control are not available in applicationWillEnterForeground. (They haven't been instantiated yet.)

    You might need more this to the viewDidLoad of the viewController.