简体   繁体   中英

Managing memory across multiple views

I'm building an app that has a main view which consists of a Map View. A second view has some necessary configuration options (config view). I segue to this config view using a partial curl segue.

The problem I'm having is the state of my config view is not being maintained.

For example,

I will segue into the config view, make some changes to the settings and return to the main Map View. Once I return to the config view again the values are back to their default values. The value in question is distanceFilterValue .

Here's the implementation of my config view controller:

@interface SimpleConfigViewController()
//private interface inside implementation
@property (weak, nonatomic) UISlider * distanceFilterSlider;
@property (strong, nonatomic) NSNumber *distanceFilterValue;
@end

@implementation SimpleConfigViewController

@synthesize distanceFilterLabel = _distanceFilterLabel;
@synthesize distanceFilterSlider = _distanceFilterSlider;
@synthesize distanceFilterValue = _distanceFilterValue;

- (NSNumber *)distanceFilterValue {
    if (!_distanceFilterValue) {
        _distanceFilterValue = [NSNumber numberWithFloat:250.0];
    }
    return _distanceFilterValue;
}

- (IBAction)distanceSliderValueChanged:(UISlider *)sender {
    self.distanceFilterValue = [NSNumber numberWithFloat:sender.value];
    //update GUI
    self.distanceFilterLabel.text = [NSString stringWithFormat:@"%.f m", sender.value];
}


@end

It seems to me that because I keep a strong pointer to distanceFilterValue, this value should be correct when I return back to config view. I'm clearly missing something here.

Thanks in advance for your help.

I could be wrong, but I guess the config view is unloaded, and loaded again from the XIB when it's pushed the second time.

You should store your values in a model object anyway and not in a controller!

I think that Erik is right, everytime you call viewDidLoad for your configView it will reset. You could use a Singleton or NSUserDefaults to solve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM