4 Replies Latest reply: May 6, 2013 8:32 AM by xnav
!# Level 1 Level 1 (0 points)

I need you help with UIProgressView.

I need update my progress view from current date to end date I choose should be.

This Method updates only label and shows how much time remain to end time circle.

 

-(void)updateProgressDate {

   
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   
int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
   
NSDateComponents *components = [cal components:units fromDate:[NSDate date] toDate:destDate options:0];
  
[_dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']];



}

-(void)viewDidLoad {

destDate 
= [NSDate dateWithTimeIntervalSince1970:1369342800];
    timer
= [NSTimer scheduledTimerWithTimeInterval:1.0

target:self

selector:@selector(updateProgressDate)

userInfo:nil

repeats:YES];


}

How I can implement NSDateComponents to UIProgressView?

Thanks


iPhone 5, iOS 6.1.3
  • xnav Level 5 Level 5 (6,635 points)
    - (void)viewDidLoad {
        [super viewDidLoad];
      self.destDate = [NSDate dateWithTimeIntervalSinceNow:300];
      //          self.destDate  = [NSDate dateWithTimeIntervalSince1970:1369342800];
      self.totalTime = [self.destDate timeIntervalSinceNow];
        [NSTimer scheduledTimerWithTimeInterval:1.0
    
                                                                                                                   target:self
    
                                                                                                           selector:@selector(updateProgressDate)
    
                                                                                                           userInfo:nil
    
                                                                                                                  repeats:YES];
    
    }
    
    -(void)updateProgressDate {
    
        NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        NSDateComponents *components = [cal components:units fromDate:[NSDate date] toDate:self.destDate options:0];
              [_dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']];
    
              float progress = 1 - [self.destDate timeIntervalSinceNow]/self.totalTime;
              _progV.progress = progress;
    
    
    
    }
    

     

    and from the .h

    @property (strong, nonatomic) NSDate *destDate;
    @property (assign, nonatomic) NSTimeInterval totalTime;
    
    @property (strong, nonatomic) IBOutlet UIProgressView *progV;
    @property (strong, nonatomic) IBOutlet UILabel *dateLabel;
    


  • !# Level 1 Level 1 (0 points)

    Thank you that helps. I will try now Understand what here done in this code

  • !# Level 1 Level 1 (0 points)

    Hi xnav

     

    Sorry Just wanted to ask another issue, it related to the same my issue bit other question

    I don't want to open a new topic for it

     

    I have a code now just upgraded it

    The first target, I need calculate range between start and current date and time and first move my progressview ahead to this date.

    On second part of my method I realized if (progV.progress == 1.0) I mean by that  progressbar finished current period and need to start new one.

    So I am recognizing how much days in a month and adding a new period by day count. I am not sure I realized it well, my billing range starting at 25 day each month and ending at 24 day next month. I conted days in current month. You can see it in my code. I'll be glad if you can help me with it and explain this subject.

    Bacause I don't know when User will open this billing.

    So I have to use some abstract date and time

    Myabe someone else can help me with this issue

     

     

    here is my code

     

     

    - (void)viewDidLoad {

        [super viewDidLoad];

       

        NSDate *currentDate = [NSDate date];

        //self.destDate = [NSDate dateWithTimeIntervalSinceNow:2];

        self.destDate  = [NSDate dateWithTimeIntervalSince1970:1369342800];

        self.totalTime = [self.destDate timeIntervalSinceNow];

       

        [NSTimer scheduledTimerWithTimeInterval:1.0

        

                                         target:self

        

                                       selector:@selector(updateProgressDate)

        

                                       userInfo:nil

        

                                        repeats:YES];

        float timeRange = [self.destDate timeIntervalSinceDate:currentDate];

       

        progV.progress = timeRange;

       

    }

     

     

    -(void)updateProgressDate {

       

        NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

        NSDateComponents *components = [cal components:units fromDate:[NSDate date] toDate:self->destDate options:0];

        [dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']];

       

        NSDate *now = [NSDate date];

       

        float progress = 1 - [self.destDate timeIntervalSinceNow]/self.totalTime;

       

        progV.progress = progress;

       

        //NSLog(@"%f", progress);

       

        if (progV.progress == 1.0) {

           

            //calculate how much days in current month

            NSCalendar *cal = [NSCalendar currentCalendar];

           

            NSRange rng = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];

           

            NSUInteger numberOfDaysInMonth = rng.length;

            // end calculating

           

            NSLog(@"%d", numberOfDaysInMonth);

           

            if (numberOfDaysInMonth == 31) {

           

                self.destDate  = [now dateByAddingTimeInterval:60*60*24*31];

               

            } else if (numberOfDaysInMonth == 30) {

           

                self.destDate  = [now dateByAddingTimeInterval:60*60*24*30];

           

            } else if (numberOfDaysInMonth == 29) {

           

                self.destDate  = [now dateByAddingTimeInterval:60*60*24*29];

           

            } else {

           

                self.destDate  = [now dateByAddingTimeInterval:60*60*24*28];

           

            }

           

            self.totalTime = [self.destDate timeIntervalSinceNow];

            progV.progress= 0;

            progV.progress=progress;

            NSLog(@"%@", now);

            NSLog(@"Progress is already 1.0!");

        }

     

    }

     

     

  • xnav Level 5 Level 5 (6,635 points)

    If I understand your question, I think this change  will help:

     

    float progress = [self.destDate timeIntervalSinceNow]/self.totalTime;
      progV.progress = 1 - progress;
      if (progress < 0) {
    .
    .
    .