Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:31:29+00:00 2026-06-14T18:31:29+00:00

Have a question on how can I update my formula by NSUserDefault. So I

  • 0

Have a question on how can I update my formula by NSUserDefault. So I have two text field which needs to keep my Formula uptodate. So the user types in there number (value) then that numbers needs to go to my formula but the formula only showing me the distance value :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:31:31+00:00Added an answer on June 14, 2026 at 6:31 pm

    I tried your code with some changes. Here are my .h file and .m files. Try this. Now also I didn’t understand what your trying to find out, but this code gives me the values not a nan. While you writing code, don’t forget to start your variable name in small letter that is a standard way. And also use self if you set a variable as property.

    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
    

    And the result in Simulator is
    result in Simulator

    Don’t forget to connect delegates and IBOutlet from interface builder.this is how you can check the delegate connection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question on how can i use core date to update my
I have a question about the datarow and how I can update it... I
I have a question that can I use AS400ConnectionPool in Spring if then pls
Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
I have question i.e how can i select a previous div CODE:- if($i >
I have a smple Question: How can I access to a class and its
I have another question and I can't seem to find anything on Google. What
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have not been able to find any definitive answers to this question: Can
I know this question can be answered by searching in google. But I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.