简体   繁体   English

跨多个视图管理内存

[英]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. 我使用部分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 . 有问题的值是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. 在我看来,由于我始终保持着指向distanceFilterValue的强大指针,因此当我返回配置视图时,该值应该是正确的。 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. 我可能是错的,但是我猜配置视图已卸载,并在第二次推送时从XIB重新加载。

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. 我认为Erik是正确的,每次您为configView调用viewDidLoad时,它将重置。 You could use a Singleton or NSUserDefaults to solve this. 您可以使用Singleton或NSUserDefaults解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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