NSUserDefault 更新公式
Posted
技术标签:
【中文标题】NSUserDefault 更新公式【英文标题】:NSUserDefault Update Formula 【发布时间】:2012-11-18 02:04:52 【问题描述】:对如何通过 NSUserDefault 更新我的公式有疑问。所以我有两个文本字段需要让我的公式保持最新。所以用户在那里输入数字(值)然后这些数字需要进入我的公式,但公式只显示距离值:)。
【问题讨论】:
公式中的值不是最新的,它显示为 nil。 请确保您保存的是正确的值类型。根据苹果的参考,NSUserDefaults 类提供了方便的方法来访问常见的类型,例如浮点数、双精度数、整数、布尔值和 URL。默认对象必须是属性列表,即以下实例(或集合实例的组合):NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary。 老兄添加一些您尝试过的代码片段,然后我们可以帮助您解决您的问题... @Programmer...,哪一行显示了问题?是在保存到 NSUserDefaults 时还是在取回时?您是否尝试在这些行上添加 NSLogs 检查问题的确切位置?您可以添加 NSLogs 并使用日志更新问题吗? 问题是获取公式的日期,我运行了一些他们说的 NSLOG(null)然后他们显示值(25 和 4.15,我在模拟器的文本字段中输入了 25 和 4.15 )。 【参考方案1】:我对您的代码进行了一些更改。这是我的.h
文件和.m
文件。试试这个。现在我也不明白你想找出什么,但这段代码给我的值不是nan
。在编写代码时,不要忘记以小写字母开头变量名,这是一种标准方式。如果您将变量设置为属性,也可以使用self
。
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController
<UITextFieldDelegate,CLLocationManagerDelegate>
IBOutlet MKMapView *mapView;
IBOutlet UITextField *textGas;
IBOutlet UITextField *textMoney;
IBOutlet UITextField *textTotal;
IBOutlet UITextField *textDistance;
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
double totalDistance;
float gas,money;
@end
@implementation ViewController
@synthesize locationManager;
- (void)viewDidLoad
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];
//set default value for Gas
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"Gas"])
[[NSUserDefaults standardUserDefaults] setObject:@"1.0" forKey:@"Gas"];
[[NSUserDefaults standardUserDefaults] synchronize];
//set default value for Money
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"Money"])
[[NSUserDefaults standardUserDefaults] setObject:@"1.0" forKey:@"Money"];
[[NSUserDefaults standardUserDefaults] synchronize];
gas = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Gas"] floatValue];
money = [[[NSUserDefaults standardUserDefaults] objectForKey:@"Money"] floatValue];
textGas.text = [NSString stringWithFormat:@"%.1f",gas];
textMoney.text = [NSString stringWithFormat:@"%.1f",money];
// Do any additional setup after loading the view, typically from a nib.
-(void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Gas"])
textGas.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"Gas"];
else
textGas.text = @"0";//set default value
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Money"])
textMoney.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"Money"];
else
textMoney.text = @"0.01";//set default value
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
[textMoney resignFirstResponder];
[textGas resignFirstResponder];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
if (buttonIndex == 1)
exit(0);
if (buttonIndex == 0)
[self.locationManager stopUpdatingLocation];
mapView.showsUserLocation = NO;
#pragma mark - UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField
if ([textField.text intValue] == 0)
textGas.text = [NSString stringWithFormat:@"%.1f",gas];
textMoney.text = [NSString stringWithFormat:@"%.1f",money];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
message:@"Value cann't be zero."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
alert = nil;
return;
if (![textField.text length])
textGas.text = [NSString stringWithFormat:@"%.1f",gas];
textMoney.text = [NSString stringWithFormat:@"%.1f",money];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
message:@"Value cann't be empty."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
alert = nil;
return;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (textField == textGas)
[defaults setObject:textGas.text forKey:@"Gas"];
gas = [textGas.text floatValue];
else if (textField == textMoney)
[defaults setObject:textMoney.text forKey:@"Money"];
money = [textMoney.text floatValue];
[defaults synchronize];
- (BOOL) textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];
return YES;
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
// Zoom to the current user location.
MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1200.0, 1200.0);
[mapView setRegion:userLocation animated:NO];
mapView.showsUserLocation = YES;
if (!oldLocation)
totalDistance = 0.0;
else
totalDistance += [newLocation distanceFromLocation:oldLocation];
double distance = totalDistance*0.00062137119;
textTotal.text = [[ NSString alloc] initWithFormat:@"$%.2f", distance/gas*money];
textDistance.text = [NSString stringWithFormat:@"%.2f Miles", distance];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
模拟器中的结果是
不要忘记从界面生成器连接delegates
和IBOutlet
。
【讨论】:
感谢您的代码,但如果用户想要更新 mpg 和 gas,仍然存在问题,此代码不允许(它显示旧值)... @Programmer... 为 mpg 和 gas 输入一个值,然后点击小键盘的一侧,它会保存这些值。 @Programmer... 我将 gas 和 mpg 的最小值设置为 1.0。尝试给出更高的值 因为 Mainstoryboard 应该在那里做点什么……(Modals 或 Push) @Programmer... 在委托-(void)textFieldDidEndEditing:(UITextField *)textField
,你可以管理你的最大值【参考方案2】:
我认为问题可能出在这里:
int gas = [[NSUserDefaults standardUserDefaults] objectForKey:@"Gas"];
int money = [[NSUserDefaults standardUserDefaults] objectForKey:@"Money"];
您直接从您的textField.text
写出您的NSUserDefaults
,通常使用0.00
格式,但您正在以int
的形式读取它。它可能存储为NSString
。您应该将其存储为NSNumber
。
进入:
NSNumber *foo = [NSNumber numberWithInteger:TextGas.text.integerValue];
NSNumber *bar = [NSNumber numberWithDouble:TextMoney.text.doubleValue];
[defaults setObject:foo forKey:@"Gas"];
[defaults setObject:bar forKey:@"Money"];
出来:
NSNumber *gas = [[NSUserDefaults standardUserDefaults] objectForKey:@"Gas"];
NSNumber *money = [[NSUserDefaults standardUserDefaults] objectForKey:@"Money"];
gas.integerValue;
money.doubleValue;
【讨论】:
从NSUserDefaults
读/写。我对您还需要什么感到困惑。
我已经向您展示了如何从NSDefaults
安全地读/写。你有责任从这里拿走它。如果您遇到问题,请将其发布,或者最好询问 TA 或同事。以上是关于NSUserDefault 更新公式的主要内容,如果未能解决你的问题,请参考以下文章