0

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

Updated:

- (void)viewDidLoad
{

    [super viewDidLoad];
// Do any additional setup after loading the view.



   //1369342800
currentDate = [NSDate date];
destDate  = [NSDate dateWithTimeIntervalSince1970:1369342800];
timeRemain = [destDate  timeIntervalSinceDate:currentDate];
timer = [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:currentDate toDate:destDate options:0];
    //[_dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components hour], 'h', [components minute], 'm']];
    [_dateLabel setText:[NSString stringWithFormat:@"%dm %dd %dh %dm", [components month], [components day], [components hour], [components minute]]];

    [_progDate setProgress:_progDate.progress = timeRemain]; // here i did += -= right now my progress bar filled but that's not right. my circle 24 day until 24 day next month

//as I understand 1% of month it's 0.03 right?

    NSLog([NSString stringWithFormat:@"Remain time: %dm %dd %dh %dm"], timeRemain);

}

so label is srill going ok, but progress bar always full.

Where I am wrong?

Thanks

Anton
  • 3,102
  • 2
  • 28
  • 47

2 Answers2

0

You need to use setProgress:.

UIProgessView takes 0.0 to 1.0 as argument. you need to calculate your remaining time as in percentage in factor of 100 and then divide it by 100.0 to get it in range 0.0 to 1.0 and pass it to setProgress:

Edit:

Instead of putting char as a parameter it can be used as :

[_dateLabel setText:[NSString stringWithFormat:@"%dm %dd %dh %dm %ds", [components month],  [components day],  [components hour],  [components minute],  [components second]]];

Also you are not supposed to pass string, You are needed to provide float. And NSTimeInterval itself is in float.

As I already said, you need to convert time into percent and then convert it into specified range.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Thanks Anoop, But the problem is here in this line: [_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']]; _progDate(my uiprogressview) can't accept strings. – Anton Apr 27 '13 at 08:29
  • Hi pls look my updated post. tell where I am wrong? Thank You – Anton Apr 28 '13 at 03:36
  • `NSLog([NSString stringWithFormat:@"Remain time: %dm %dd %dh %dm"], timeRemain);` specifiers are 4, but you passed only one. – Anoop Vaidya Apr 28 '13 at 06:40
  • HI, by other forums and my game with a code I got the progress view by date, but I have an issue with adding future period `self.destDate = [now dateByAddingTimeInterval:60*60*24*31];` sometimes month has 31, 30, 28 or 29 days (February only). How I can know how much days in current month and do `if (day == 31) { self.destDate = [now dateByAddingTimeInterval:60*60*24*31]; } else { self.destDate = [now dateByAddingTimeInterval:60*60*24*30]; } ` – Anton May 05 '13 at 19:36
  • Found how now half work working good NSCalendar *cal = [NSCalendar currentCalendar]; NSRange rng = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]]; NSUInteger numberOfDaysInMonth = rng.length; – Anton May 05 '13 at 19:51
0

So Here is right code, at least I can now calculate elapsed time and send it to progress view:

  -(void)billRangeOperations {

    today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd"];
    NSString *currentDateFormated = [dateFormatter stringFromDate:today];
    int dateFormated = [currentDateFormated intValue];
    NSLog(@"Today is: %i", dateFormated);

    //calculate how much days in current month ******************************************


    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    int units = NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps = [cal components:units fromDate:today];
    NSRange days = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:today];
    NSUInteger numberOfDaysInMonth = days.length;

    NSLog(@"%i Days in current month: %i", numberOfDaysInMonth, comps.month);

    // end calculating ******************************************************************

    // calculating elapsed billing range ************************************************

    //dateFormated = 26; //Uncomment this string to check manually, how your billing works if date will be from 25 day of month !!!!!!!!!!!!!!!!!!!!!!!!


    if (dateFormated < 25) {

        int remainDaysInCurrentMonth = days.length - 25;

        NSLog(@"Days Remain to the end of month: %i", remainDaysInCurrentMonth);

        int elapsedTime = remainDaysInCurrentMonth + dateFormated;

        NSLog(@"Elapsed Days: %i", elapsedTime);

        float progress = elapsedTime * 100 / days.length;

        NSLog(@"Progress Days: %f", progress);

        float progressTimeFormated = progress / 100;

        NSLog(@"Going to ProgressView: %f", progressTimeFormated);

        // *************************************************************

        [progV setProgress:progressTimeFormated animated:YES];

        // remain days to the end period to text string *************************************

        int remainDaysPeriod = 25 - dateFormated;

        remainDays.text = [NSString stringWithFormat:@"%i", remainDaysPeriod];

        // **********************************************************************************

    } else if (dateFormated >= 25) {

        //***** calculating days in next month *************************

        comps.month = comps.month+1;
        NSRange range = [cal rangeOfUnit:NSDayCalendarUnit
                                  inUnit:NSMonthCalendarUnit
                                 forDate:[cal dateFromComponents:comps]];
        NSLog(@"%i Days in next month: %i", range.length, comps.month);

        //**************************************************************


        int remainDaysInCurrentMonth = range.length - 25;


        NSLog(@"Days Remain in current month: %i", remainDaysInCurrentMonth);

        int elapsedTime = dateFormated - 25;

        NSLog(@"Elapsed Time: %i", elapsedTime);

        float progress = elapsedTime * 100 / range.length;

        NSLog(@"Progress time: %f", progress);

        float progressTimeFormated = progress / 100;

        NSLog(@"Going to ProgressView: %f", progressTimeFormated);

        // end calculating elapsed billing range

        [progV setProgress:progressTimeFormated animated:YES];

        // remain days to the end period ********************************************************


        int remainDaysPeriod = remainDaysInCurrentMonth+25-elapsedTime; 

        remainDays.text = [NSString stringWithFormat:@"%i", remainDaysPeriod];

        // **************************************************************************************


        NSLog(@"More than 25");

    }

}
Anton
  • 3,102
  • 2
  • 28
  • 47