Reputation: 2283
I am having little bit confused regarding this way of asking question
Any how i am mentioning my query with support of image here. Can you please help me out regarding this issue
Step 1: I have an requirement like: By using CLLocationManger delegate methods i fetched speed value like:
- (void)startLocationUpdates{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = YES;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
#pragma mark
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
float speed = locationManager.location.speed;
float speedInKmph = speed * 3.6; // to convert the speed into kmph.
NSString *speedValue =[NSString stringWithFormat:@"%.f Kmph ",speedInKmph];
self.currentSpeedLblRef.text = speedValue;
self.maxSpeedLblRef.text = speedValue;
}
Step 2: currentSpeedLblRef,maxSpeedLblRef --> these are my UILabels
Example: Now i am driving a car --> For the first time i opened the app and i got the current speed of car(like: 120 Kmph) and then i need to display same value in "maxSpeedLblRef"(120 Kmph) also
After some time my current speed of car if 50 Kmph. But i need to display value in "maxSpeedLblRef" is --> Max value --> means 120 kmph . Because already i was getting 120 kmph value for previos
After that If my current speed of car if 180 kmph --> I need to show "maxSpeedLblRef" valu like: 180 Kmph. Because it is the latest one compare to 120 Kmph
After Close the app and then
If i reopen the app i want to show the vale like "maxSpeedLblRef" --> 180 Kmph. Because this value is the previous saved value
HERE IS MY SOURCE CODE:Click here link
Upvotes: 0
Views: 78
Reputation: 5554
you are missing a few simple things here!
You need to store the max speed for comparison - all you're doing here is is updating the label with the current value every time. Set up a class-level attribute, initial value = 0, and update it whenever current speed > max speed.
There are a few ways you can store the max value so that it's there when you next open the app. Probably easiest to go with user defaults. There are many tutorials available - try this one http://code.tutsplus.com/tutorials/ios-sdk-working-with-nsuserdefaults--mobile-6039
OK - using your project code, here's what you need.
Update your ViewController.h to this
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *currentSpeedLblRef;
@property (weak, nonatomic) IBOutlet UILabel *maxSpeedLblRef;
// you need to store the max speed
@property float speedMax;
-(void)loadDefaults;
-(void)storeDefaults;
@end
and then in the VIewController.m, replace the viewDidLoad with this
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self loadDefaults];
[self startLocationUpdates];
}
update the locationManager function to this
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
float speed = locationManager.location.speed;
float speedInKmph = speed * 3.6; // to convert the speed into kmph.
NSString *speedValue =[NSString stringWithFormat:@"%.f Kmph ",speedInKmph];
self.currentSpeedLblRef.text = speedValue;
if (speedInKmph > self.speedMax)
{
self.speedMax = speedInKmph;
[self storeDefaults];
}
NSString *speedMaxValue =[NSString stringWithFormat:@"%.f Kmph ",self.speedMax];
self.maxSpeedLblRef.text = speedMaxValue;
}
and, finally add the load / store functions
- (void)loadDefaults
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.speedMax = [defaults floatForKey:@"SpeedMax"];
}
- (void)storeDefaults
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setFloat:self.speedMax forKey:@"SpeedMax"];
[defaults synchronize];
}
Upvotes: 1