在一个视图控制器中还原多个文本字段和标签

时间:2014-02-13 04:55:55

标签: ios objective-c uitextfield uisegmentedcontrol state-restoration

我有一个视图控制器,包含2个文本字段,2个分段控件和一些标签,用于显示用户使用UIButtons设置的时间戳。我希望能够在退出/重启时以及返回到我的主菜单view controllernavigation controller时恢复这些项目的任何用户设置值。使用下面的代码,我能够恢复background / terminate / restart上的一个文本字段,但我不确定如何为我的其他文本字段或时间戳标签和分段控件完成此操作。我试图复制恢复代码,更改文本字段名称和@"UnsavedText"字符串,没有运气。

此外,每当我使用导航栏中的后退按钮返回主菜单时,我都会丢失所有字段中的所有数据。

以下是我的代表中的代码,选择状态恢复:

// Sets RESTORATION
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
    return YES;
}

-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
    return YES;
}

以下是我的视图控制器中恢复一个文本字段的相关代码。我将viewDidLoad代码包含在状态初始化中,以防这是问题的一部分:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Updates time on currentTimeLabel
    [self updateTime];

    // Sets initial button states
    [self setInitialState];

    // Sets UITextField delegate
    self.startLevel.delegate = self;
    self.stopLevel.delegate = self;
    // initializes basic values for segmented controls
    bigTank = YES;
    startFractions = @"";
    stopFractions = @"";


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// Restoration of text fields
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    // start level text
    [coder encodeObject:_startLevel.text forKey:@"UnsavedText"];
    [super encodeRestorableStateWithCoder:coder];

}

-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
    // start level text
    _startLevel.text = [coder decodeObjectForKey:@"UnsavedText"];
    [super decodeRestorableStateWithCoder:coder];

}



// Sets initial button and label states
- (void)setInitialState
{
    self.start.enabled = YES;
    self.stop.enabled = NO;
    self.calculate.enabled = NO;
    self.resume.enabled = NO;

    // Text for time labels reset
    _startTimeLabel.text = @"- -:- -:- -.- -";
    _stopTimeLabel.text = @"- -:- -:- -.- -";

    // Resets the minuteRateLabel
    _minuteRateLabel.text = @"--.--";

    // Resets the text fields to their initial state with the placeholder "inches" text
    [_startLevel setText:nil];
    [_stopLevel setText:nil];

    // Resets inch levels to EVEN
    [self.startFractionControl setSelectedSegmentIndex:0];
    [self.stopFractionControl setSelectedSegmentIndex:0];
    startFractions = @"";
    stopFractions = @"";
}

提前致谢!

编辑:

这是我根据建议尝试的代码:

我的按钮点击表面上应该保存时间戳值:

- (IBAction)startButton:(UIButton *)sender
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"hh:mm:ss.SS"];
    _startTimeLabel.text = [formatter stringFromDate:[NSDate date]];
    UIButton *startButton = (UIButton *)sender;
    // Creates the start time stamp for use in the calculation
    startTime = [NSDate timeIntervalSinceReferenceDate];
    // sets button states
    startButton.enabled = NO;
    stop.enabled = YES;
    // Hides keypad on startButton click
    [self.startLevel resignFirstResponder];
    [self.stopLevel resignFirstResponder];

    // saves the value for restoration using user defaults
    NSDateFormatter *startTimeSave = formatter;
    [[NSUserDefaults standardUserDefaults]
     setObject:startTimeSave forKey:@"startTimeSaver"];
    }

1 个答案:

答案 0 :(得分:0)

您可以使用用户默认值保存文本值。以下是示例代码段:

NSString *valueToBeSaved = @"someValue";
[[NSUserDefaults standardUserDefaults]
setObject:valueToBeSaved forKey:@"preferenceName"];

要在以后检索您的值,请使用:

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferenceName"];