Obj C - 加载视图后,NSUserDefaults不会返回要计算的值

时间:2015-07-22 11:51:41

标签: ios objective-c uiviewcontroller nsuserdefaults

我几乎没有问题。我有基本的计算应用程序。

Viewcontroller.m

#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate, UIAlertViewDelegate>
@end

@implementation ViewController
-(void)textFieldDidEndEditing:(UITextField *)textField
{

    self.currentSettings = _currentSettings;
    [self calculateThePrice];
}

-(void)calculateThePrice
{
    float wynik = self.currentSettings.kwh * self.currentSettings.price;
    self.priceLabel.text = [NSString stringWithFormat:@"%.02f %@", wynik , self.currentSettings.currency];
}

SettingsVC.m

#import "SettingsVC.h"
@interface SettingsVC () <UITextFieldDelegate>
@end

@implementation SettingsVC
#pragma mark - UserDefaults Implementation

-(void)viewWillAppear:(BOOL)animated
{
    [self createCurrencyArray];

    NSUserDefaults *priceDef = [NSUserDefaults standardUserDefaults];
    NSString *priceDefText = [priceDef stringForKey:@"priceCall"];

    _priceTextField.text = priceDefText;   

    NSUserDefaults *currencyDef = [NSUserDefaults standardUserDefaults];
   [_currencyPicker selectRow:[currencyDef integerForKey:@"currencyCall"]
         inComponent:0 animated:NO];
    [priceDef synchronize];
    [currencyDef synchronize];
}

-(void)viewWillDisappear:(BOOL)animated
{
    NSString *textOfPriceTexField = _priceTextField.text;
    [[NSUserDefaults standardUserDefaults] setObject:textOfPriceTexField forKey:@"priceCall"];
}

现在,问题是当我希望程序自动计算时,它不会。要得到任何结果,我必须切换到第二视图,从选择器中选择一个值,然后当我回去时,我有我的结果。 但...   - 当我在第一个屏幕上更改值时,结果不会改变。当我在第二个scree上更改值时,结果不会改变。但是当我改变PickerView上的值 - TADAH - 结果更新!

  • 当我进入第二个视图,然后返回第一个视图,然后再次返回第二个并返回第一个视图,我的结果更改为“0.00(NULL)”...

我做错了什么想法?我认为这是关于NSUserDefaults,我尝试了很多选项,没有任何效果,也没有改变任何东西。

2 个答案:

答案 0 :(得分:0)

设置新值时,需要同步NSUserDefaults。您在检索值时使用同步。

这里你不需要2个默认指针,也不需要同步:

NSUserDefaults *priceDef = [NSUserDefaults standardUserDefaults];
NSString *priceDefText = [priceDef stringForKey:@"priceCall"];

_priceTextField.text = priceDefText;   

[_currencyPicker selectRow:[priceDef integerForKey:@"currencyCall"] inComponent:0 animated:NO];

您需要同步:

-(void)viewWillDisappear:(BOOL)animated 
{

    NSString *textOfPriceTexField = _priceTextField.text;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:textOfPriceTexField forKey:@"priceCall"];
    [defaults synchronize]; 
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSInteger selectedRow = [_currencyPicker selectedRowInComponent:0];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:selectedRow forKey:@"currencyCall"];
    [defaults synchronize];

    self.currentSettings.currency = [self.currencyArray objectAtIndex:row];
    self.currentSettings.price = [self.priceTextField.text floatValue];

    //[self userDidFinishSetting];
}

答案 1 :(得分:0)

试试此代码

-(void)viewWillDisappear:(BOOL)animated
{
     NSString *textOfPriceTexField = _priceTextField.text;
     [[NSUserDefaults standardUserDefaults] setObject:textOfPriceTexField forKey:@"priceCall"];
}
-(void)viewWillAppear:(BOOL)animated
{
    [self createCurrencyArray];  
    NSUserDefaults *priceDef = [NSUserDefaults standardUserDefaults];
    NSString *priceDefText = [priceDef objectForKey:@"priceCall"];
}

而不是stringForKey,你可以调用objectForKey和integerForKey相同,你也可以调用objectForKey。实际上,您设置的对象不是字符串或整数。