我有一个计算器,可以将某个值乘以一定的美元汇率。用户可以编辑 $ 汇率,该值与用户输入的汇率相乘。这是我的代码:
- (IBAction)edit
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Edit New Amount"
message:@"Enter new rate"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex) {
UITextField *field = [alertView textFieldAtIndex:0];
field.placeholder = @"Enter New Rate";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
if ([field.text rangeOfCharacterFromSet:set].location != NSNotFound)
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Only numbers are allowed in this field."delegate:self cancelButtonTitle:@"OK."otherButtonTitles:nil];
[errorAlert show];
rate.text=@"";
}
else
{
rate.text = field.text;
}
}
else
{
//Cancel
}
}
但是,当应用程序关闭并重新启动时,会显示旧费率,而不是用户输入的新费率。如何保存新的用户输入费率,以便它保留在应用程序中,即使重新启动也是如此?