2

in my app I have a UIProgressView that shows the time elapsed between two NSDate. Everything works properly, each minute that passes UIProgressView advances by 0.1 per minute.

The difference between the two dates is calculated in this way with a CGFloat

Besides that I have a UIButton it should have the function of "decrease the ProgressView 0.1" in a few words each time the button is pushed I would like to take one minute to the difference between the two NSDate that I created previously. I made a few attempts but I can not get this because when my app is closed and then reopened the time difference between the two dates do not change ... how can I do this?

I show you the code I'm using

-(void)viewDidLoad {
    [super viewDidLoad];
    [self startDate];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self upgradeEnergyBar];
    [NSTimer scheduledTimerWithTimeInterval: (1 * 60)
                                     target:self
                                   selector:@selector(upgradeEnergyBar)
                                   userInfo:nil
                                    repeats:YES];
}

-(void)startDate {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    if (! [defaults boolForKey:@"notFirstRun"]) {
        NSDate *date = [NSDate new];
        [[NSUserDefaults standardUserDefaults] setObject:date forKey:@"open"];
        NSLog(@"Ora di apertura : %@", date);
        [defaults setBool:YES forKey:@"notFirstRun"];
    }
}

- (IBAction)sfAction:(id)sender {
    //Decrement CGFLOAT
}

-(void)upgradeEnergyBar {
    self.now = [NSDate new];
    NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"open"];
    NSTimeInterval timeInterval = [self.now timeIntervalSinceDate:lastDate];
    _minutes = timeInterval / 60;

    _energyBar.progress = _minutes * 0.1;
    [self updateLabelEnergyWithProgress:_energyBar.progress];
    _minute.text = [NSString stringWithFormat:@"MINUTES:%ld", (long) _minutes];


    NSLog(@"MINUTES:%ld", (long) _minutes);

    if (_energyBar.progress == 1) NSLog(@"end");
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
kAiN
  • 2,559
  • 1
  • 26
  • 54

1 Answers1

1

You seem to have a couple questions in there, but to answer your "how to Subtract a minute" ...

What you really want to do is ADD a minute to your saved date. So, on sfAction: you could:

- (IBAction)sfAction:(id)sender {

    // get the 'saved' date/time
    NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"open"];

    // add one minute (60 seconds)
    lastDate = [lastDate dateByAddingTimeInterval:60];

    // get the current date/time
    NSDate *now = [NSDate date];

    // use the earlier date/time (to avoid a date/time in the future)
    NSDate *earlier = [lastDate earlierDate:now];

    // save it back to User Defaults
    [[NSUserDefaults standardUserDefaults] setObject:earlier forKey:@"open"];

}

Edit: This now includes code to make sure we're not incrementing the saved date/time to be later than the current date/time.

Side note...

It's really not a good idea to constantly read/write User Defaults.

You should read the values when your app launches (or perhaps also when it returns to the foreground) and save the values in variables / properties.

When your app is exiting (or being sent into the background) you can write the updated values.

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Works perfectly ... Now I cpaito my mistake ... the problem is I do not save my skin again the new NSUserDefault .. I want to ask a question ... The code works fine when I open and close my app now minutes change ... but why start my application from XCode not close it by ipad the minutes do not change? – kAiN Mar 23 '17 at 14:36
  • You .. for the negative value I Fixed in this way if (bar.progress == 0) return; can you tell me if and The Way to avoid the negative value? – kAiN Mar 23 '17 at 14:41
  • I edited my answer to show one way to avoid the negative / future time issue. I'm not really sure what your other question is though... – DonMag Mar 23 '17 at 15:14
  • I noticed that created another problem ... because when my UIProgressView reaches 0 hours stops and my NSDate is not bad .. but now you create the problem when my UIProgressView reaches 100%, but my NSDate continues to increase the minutes between the two dates, but it should stop when the progressView reaches 100% in a few words .. I the inverse problem in the method - (void) upgradeEnergyBar ... at this point, how can I fix it – kAiN Mar 23 '17 at 15:30
  • Also what do you mean "It's really not a good idea to constantly read/write User Defaults. You should read the values when your app launches (or perhaps also when it returns to the foreground) and save the values in variables / properties." – kAiN Mar 23 '17 at 15:36
  • in AppDelegate? – kAiN Mar 23 '17 at 16:19
  • We're getting into topics that require learning... can't be answered in one or two lines. I have no idea what else your app should be doing, but I put together a simple example that should answer some of your remaining questions... https://github.com/DonMag/OCEnergyTimer – DonMag Mar 23 '17 at 16:49
  • with Your example, you gave me saved my life !!! are two days that I slam my head on the monitor for this thing !!! I regret not being able to give you more votes !!!! my UIProgressView is an energy recharge for the user of my app !!! This was precisely what I meant! Thank you so much you were very very kind, I hope I can repay a kindness :) – kAiN Mar 23 '17 at 17:19
  • the count of 1 countdown just minutes you should get easily right? – kAiN Mar 23 '17 at 17:55
  • it would be advisable to fit 2 NSTimer same ViewController? I wanted to try to get unu count of 1 minute timer for each advance of my UIProgressView – kAiN Mar 23 '17 at 19:12
  • You can use two NSTimers.. but you might want to use a quicker timer and do one thing on the "minute" mark and something else on the other times. – DonMag Mar 23 '17 at 19:31
  • you mean .. use the timer that controls the update of the progress bar ?? that you wanted to say ... in this way it would be a NSTimer? ... Even ... it creates big problems to 8 UIButton to use the system (for each button) used to decrease the UIProgressView ?? .. Sorry for all these questions, maybe even stupid, but I wanted to be sure there are no errors – kAiN Mar 23 '17 at 19:36
  • I wanted to say that I still have problems to create a countdown _lastdate wanted to do in a UILabel display the time remaining for the completion of uiprogressview ... also I own 8 buttons and each button has to decrease my uiprogressview and wanted to know if this creates problems memory or other – kAiN Mar 23 '17 at 21:05
  • hmm... too complicated for this forum... if you want to make your project available, I can take a look. But we're beyond the scope of a SO question & answer. – DonMag Mar 23 '17 at 23:06
  • I do not want to take advantage of your kindness .. you've been very kind to me ... my questions are only for the safety of my applications, because they do not know if sometimes what you think is right or wrong .. – kAiN Mar 24 '17 at 07:13
  • one of the problems I've noticed is when the uiprogressview reaches 100% the time between the two dates does not stop .. When I take off subtract diminisce not 100% because the time between the two dates continued to flow – kAiN Mar 24 '17 at 09:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138933/discussion-between-donmag-and-sockar). – DonMag Mar 24 '17 at 12:14