2013-04-27 47 views
0

我需要你幫助UIProgressView。xcode set progressView結束日期

我需要更新我的進度視圖從當前日期到我選擇的結束日期應該是。

此方法只更新標籤並顯示剩餘多少時間來結束時間圓。

-(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]; 


} 

我如何實現NSDateComponents到UIProgressView?

感謝

更新時間:

- (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 

//我瞭解月份的1%,這是0.03嗎?

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

} 

所以標籤是srill會好的,但進度條總是滿的。

我在哪裏錯了?

謝謝

回答

0

您需要使用setProgress:

UIProgessView以0.0到1.0作爲參數。你需要100.0計算你的剩餘時間,在100因子百分比,然後除以它得到它的範圍在0.0到1.0,並將它傳遞給setProgress:

編輯:

而不是把字符作爲參數的它可以用作:

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

另外你不應該傳遞字符串,你需要提供浮動。而NSTimeInterval本身是浮動的。

正如我已經說過的,您需要將時間轉換爲百分比,然後將其轉換爲指定的範圍。

+0

感謝Anoop,但問題是這裏在這一行:[_dateLabel的setText:[的NSString stringWithFormat :[「元件月」,「m」,「元件日」,「d」,「元件小時數」,「h」 ',[組件分鐘],'米',[組件秒],''']]; _progDate(我的uiprogressview)不能接受字符串。 – Anton 2013-04-27 08:29:17

+0

@Anton:檢查更新/編輯 – 2013-04-27 17:28:48

+0

謝謝!我會嘗試你的解決方案,並會更新你。 – Anton 2013-04-27 18:21:33

0

所以這是正確的代碼,至少我現在可以計算經過的時間,並將其發送給發展觀:

-(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"); 

    } 

}