NSUserDefault で数式を更新する方法について質問があります。そのため、フォーミュラを最新の状態に保つ必要がある 2 つのテキスト フィールドがあります。したがって、ユーザーはそこに数値(値)を入力し、その数値を数式に入力する必要がありますが、数式は距離値のみを表示します:)。
質問する
368 次
2 に答える
2
いくつかの変更を加えてコードを試しました。これが私の.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
于 2012-11-18T04:19:47.710 に答える