我简化了提问的代码。
我想保存更多类并加载类模型文件。
但是现在我将参数简化为模型(FlightSettingModel.h)中的一部分(lim)。
在 FlightSettingModel.h 中,我在这里设置了一些参数。
FlightSettingModel.h 代码如下:
#import <Foundation/Foundation.h>
@interface FlightSettingModel : NSObject<NSCoding>
// lim property part
@property float limCurrentVal;
@property float limDefaultVal;
@property float limMaxVal;
@property float limMinVal;
// .... other property part
@end
FlightSettingModel.m 代码如下
#import "FlightSettingModel.h"
@interface FlightSettingModel()
{
}
@end
@implementation FlightSettingModel
-(instancetype)init
{
self = [super init];
if(self)
{
self.limDefaultVal = 3.0;
self.limCurrentVal = 4.0;
self.limMaxVal = 10;
self.limMinVal = 0;
// ... other property part value init .....
}
return self;
}
- (void)setFlightSettingToDefaultValue
{
self.limCurrentVal = self.limDefaultVal;
}
- (void) encodeWithCoder: (NSCoder *)encoder
{
[encoder encodeFloat:self.limCurrentVal forKey:@"limCurrentVal"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if((self = [super init])) {
self.limCurrentVal = [decoder decodeFloatForKey:@"limCurrentVal"];
}
return self;
}
@end
然后我将单例 SettingData 文件设置为初始化 FlightSettingModel 和其他模型类。
SettingData 模型头如下:
#import <Foundation/Foundation.h>
#import "FlightSettingModel.h"
@interface SettingData : NSObject
@property (nonatomic,strong) FlightSettingModel *flightSettingModel;
+(SettingData*) sharedInstance;
@end
SettingData.m 代码如下:
#import "SettingData.h"
@implementation SettingData
SettingData *sharedInstance;
+(SettingData*) sharedInstance{
if( sharedInstance == nil )
{
sharedInstance = [SettingData new];
}
return sharedInstance;
}
-(id) init{
self = [super init];
if( self )
{
self.flightSettingModel = [FlightSettingModel new];
}
return self;
}
@end
在我的故事板(UI)中如下所示:
当我单击保存按钮时,我想将自定义类模型(FlightSettingModel.h)保存在 NSKeyedArchiver 中。当我单击加载时,我想使用 NSKeyedUnarchiver 从存档器加载模型并重置为滑块。
但是现在,当我将滑块拖动到其他值(例如:10)时,然后单击保存,然后关闭应用程序重新启动应用程序。我单击加载,滑块值将变为 0。
我不知道为什么当我加载值时,所有值都会变为 0;
我的视图控制器代码 .m 下面:
#import "ViewController.h"
#import "SettingData.h"
@interface ViewController ()
{
NSString *path;
NSString *fileName;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// file manage
path =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
fileName = [path stringByAppendingPathComponent:@"flightFile"];
[self setUIValue];
NSLog(@"---");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)saveAction:(UIButton *)sender {
[NSKeyedArchiver archiveRootObject:[SettingData sharedInstance].flightSettingModel toFile:fileName];
}
- (IBAction)loadAction:(UIButton *)sender {
[SettingData sharedInstance].flightSettingModel = (FlightSettingModel*) [NSKeyedUnarchiver unarchiveObjectWithFile: fileName];
[self setUIValue];
NSLog(@"current Value:%.f",[SettingData sharedInstance].flightSettingModel.limCurrentVal);
}
- (IBAction)sliderChangedAction:(UISlider *)sender {
[SettingData sharedInstance].flightSettingModel.limCurrentVal = sender.value;
self.theTextField.text = [NSString stringWithFormat:@"%.f",self.theSlider.value];
}
-(void) setUIValue
{
// setting slider property
self.theSlider.maximumValue = [SettingData sharedInstance].flightSettingModel.limMaxVal;
self.theSlider.minimumValue = [SettingData sharedInstance].flightSettingModel.limMinVal;
self.theSlider.value = [SettingData sharedInstance].flightSettingModel.limCurrentVal;
self.theTextField.text = [NSString stringWithFormat:@"%.f",self.theSlider.value];
}
@end
有谁知道我的代码中的问题在哪里?
非常感谢您。
如果你想下载完整的代码(问题代码),我已经上传到github