请问如何通过NSUserDefault更新我的公式。所以我有两个文本字段需要保持我的公式uptodate。所以用户输入数字(值),然后该数字需要去我的公式,但公式只显示距离值:)。NSUserDefault更新公式
回答
我试着用一些改变你的代码。这里是我的.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和天然气这个代码不允许它(它显示旧值)仍然有问题... –
如果你想我会立即给你我的代码... –
那么我们应该怎么做呢? –
我认为这个问题可能在于这里:
int gas = [[NSUserDefaults standardUserDefaults] objectForKey:@"Gas"];
int money = [[NSUserDefaults standardUserDefaults] objectForKey:@"Money"];
您从textField.text
与0.00
格式写出你NSUserDefaults
直接,经常,但你在阅读它作为一个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;
那么我们应该怎么做才能解决这个问题... –
增加了回答代码。 –
我应该在哪里把所有这些代码解释得很少...... –
- 1. 如何更新NSUserDefault
- 2. swift:如何更新NSUserDefault?
- 3. C#更新公式
- 4. 复制并更新公式
- 5. 自动更新公式
- 6. Excel:更新单元格的公式
- 7. JAVA Excel POI API:公式未更新
- 8. Excel公式不更新单元格
- 9. 用数学公式更新数据库
- 10. 使用VBA条件更新公式
- 11. 如何更新条件公式?
- 12. Excel公式不更新自动
- 13. 使用Excel公式更新表格
- 14. sheet_name.calculate不更新公式,但shift + f9更新?
- 15. PCT变更公式
- 16. 从NSUserDefault
- 17. 在NSUserDefault
- 18. 尝试更新CLLocation,如果NSUserDefault存在于Swift中
- 19. 在应用程序更新后处理缺少NSUserDefault键
- 20. 从NSUserDefault读取和更新字典类型PSMultiValueSpecifier
- 21. 如何获取iOS 4上的更新NSUserDefault?
- 22. Excel公式不会更新,除非我单击公式并按回车键
- 23. 使用公式中的另一个范围更新一系列公式
- 24. Excel:如何让公式相对于每个新的公式组更改?
- 25. 在java中更快地重新计算公式给我更快
- 26. 编程式分配的公式未更新
- 27. 如何检测NSUserDefault中的更改?
- 28. Linkedin公司页面更新
- 29. 更新公共字符串
- 30. PHP - 更新公共变量
而问题是什么? –
该值在公式中没有得到更新,它显示nil。 –
请确保您正在保存正确的值类型。根据apple的参考资料,NSUserDefaults类提供了访问常见类型的便捷方法,例如浮点数,双精度,整数,布尔值和URL。一个默认对象必须是一个属性列表,也就是一个实例(或集合的实例组合):NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary。 – Mehfuz