2013-07-07 85 views
0

我有一個導航控制器與根視圖控制器是一個表視圖控制器。當你點擊一個單獨的單元格時,會有一個定時器(帶有標籤倒計時)以及開始,暫停和繼續按鈕。當視圖加載時,暫停和繼續按鈕被隱藏。當你點擊開始按鈕時,它應該消失並顯示暫停和繼續按鈕,同時定時器將開始倒計時。出於某種原因,當我點擊導航控制器,並重新點擊相同的單元格時,沒有任何屬性被加載(標籤沒有倒計時時間,並且即使應該隱藏起動按鈕仍然存在,和暫停/繼續按鈕不在那裏,即使他們應該在那裏)。我如何使它如此輕敲一個單元格,然後重新點擊它顯示相同的屬性? (我不認爲代表團正確的,因爲它不是一個模式VC)在導航控制器中保存視圖之間的屬性?

這裏是我的代碼:

propertiesviewcontroller.m(顯示的內容,你點擊一個細胞後)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [_timeLeft setFont:[UIFont fontWithName:@"BebasNeue" size:25]]; 
    //if (!([components hour] && [components minute] && [components second])) 
    if (startButton.hidden == NO){ 
     [pauseButton setHidden:YES]; 
    [continueButton setHidden:YES]; 
    } else { 
     [pauseButton setHidden:NO]; 
     [continueButton setHidden:NO]; 
    } 
} 
-(IBAction)startTimer:(id)sender{ 
    [sender setHidden:YES]; 
    [pauseButton setHidden:NO]; 
    [continueButton setHidden:NO]; 
     gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
     self.date1 = [NSDate dateWithTimeInterval:[testTask timeInterval] sinceDate:[NSDate date]]; 

     timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; 
    [timer fire]; 

} 
-(void)timerAction:(NSTimer *)t{ 
    NSDate *now = [NSDate date]; 
    components = [gregorianCalendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now toDate:self.date1 options:0]; 

    NSString *timeRemaining = nil; 
    if([now compare:self.date1] == NSOrderedAscending){ 
     timeRemaining = [NSString stringWithFormat:@"%02d:%02d:%02d", [components hour], [components minute], [components second]]; 
     NSLog(@"works %@", timeRemaining); 
    } else { 
     timeRemaining = [NSString stringWithFormat:@"00:00:00"]; 
     [self.timer invalidate]; 
     self.timer = nil; 
     if (self.alertView == NULL){ 
     self.alertView = [[UIAlertView alloc]initWithTitle:[testTask taskName] message:@"Time is up!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];  
     NSLog(@"ended"); 
     } 
    } 
    timerLabel.text = timeRemaining; 
    [timerLabel setNeedsDisplay]; 
    [self.view setNeedsDisplay]; 
} 

- -EDIT-- viewWillAppear中更新

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


    if (startButton.hidden == NO){ 
     [pauseButton setHidden:YES]; 
     [continueButton setHidden:YES]; 
    } else { 
     [pauseButton setHidden:NO]; 
     [continueButton setHidden:NO]; 
    } 
     timeRemaining = [NSString stringWithFormat:@"%02d:%02d:%02d", [components hour], [components minute], [components second]]; 
    timerLabel.text = timeRemaining; 
    [timerLabel setNeedsDisplay]; 
    [self.view setNeedsDisplay]; 
} 

- 編輯2-- TableViewController.m

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
cellSubclassCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 
    if (!cell) 
     cell = [[cellSubclassCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"]; 

    if([indexPath section] == 0){ 
    cell.textLabel.text = [[[self.taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString]; 

    cell.imageView.image = [UIImage imageNamed:@"unchecked.png"]; 
     cell.imageView.highlightedImage = [UIImage imageNamed:@"uncheckedhighlighted.png"]; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     [cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]]; 
     cell.textLabel.textColor = baseColor; 

     NSString *detailText = [[self.taskArray objectAtIndex:[indexPath row]] timeIntervalString]; 
     cell.detailTextLabel.text = detailText; 
       [[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:12]]; 
     [[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:16]]; 
[cell.contentView setAlpha:1]; 
    } else if ([indexPath section] == 1) { 
    cell.textLabel.text = [[[self.completedArray objectAtIndex:[indexPath row]] taskName] uppercaseString]; 

    cell.imageView.image = [UIImage imageNamed:@"checked.png"]; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     [cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]]; 
     cell.textLabel.textColor = baseColor; 
     NSString *detailText = [[self.completedArray objectAtIndex:[indexPath row]] timeIntervalString]; 
     cell.detailTextLabel.text = detailText; 
     [[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:12]]; 
     [[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:16]]; 
     [cell.contentView setAlpha:0.5]; 
    } 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handlechecking:)]; 
    //cell.contentView 
    [cell.imageView addGestureRecognizer:tap]; 
    cell.imageView.userInteractionEnabled = YES; 
    return cell; 
    } 
-(void)handlechecking:(UITapGestureRecognizer *)t{ 

     CGPoint tapLocation = [t locationInView:self.tableView]; 
     NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation]; 
     NSIndexPath *newIndexPath = nil; 

     if (tappedIndexPath.section == 0) { 

      NSUInteger newRowIndex = self.completedArray.count; 
      [self.completedArray addObject:[self.taskArray objectAtIndex:tappedIndexPath.row]]; 
      [self.taskArray removeObject:[self.taskArray objectAtIndex:tappedIndexPath.row]]; 
      newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:1]; 

     } else { 

      NSUInteger newRowIndex = self.taskArray.count; 
      [self.taskArray addObject:[self.completedArray objectAtIndex:tappedIndexPath.row]]; 
      [self.completedArray removeObject:[self.completedArray objectAtIndex:tappedIndexPath.row]]; 
      newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0]; 
     } 
     [self.tableView beginUpdates]; 
     [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView deleteRowsAtIndexPaths:@[tappedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     [self.tableView endUpdates]; 

} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    NSInteger num = 0; 
    if (section == 0) { 
     num = self.taskArray.count; 
    } else { 
     num = self.completedArray.count; 
    } 
    return num; 
} 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    Tasks *task = [[Tasks alloc]init]; 
    if (indexPath.section == 0){ 
    task.taskName = [[self.taskArray objectAtIndex:[indexPath row]] taskName]; 
     task.timeInterval = [[self.taskArray objectAtIndex:[indexPath row]] timeInterval]; 
    task.dateCreated = [[self.taskArray objectAtIndex:[indexPath row]] dateCreated]; 
    } else if (indexPath.section == 1){ 
     task.taskName = [[self.completedArray objectAtIndex:[indexPath row]] taskName]; 
     task.timeInterval = [[self.completedArray objectAtIndex:[indexPath row]] timeInterval]; 
     task.dateCreated = [[self.completedArray objectAtIndex:[indexPath row]] dateCreated]; 
    } 
    DetailViewController *dvc = [[DetailViewController alloc]init]; 
    [dvc setTestTask:task]; 
    [[self navigationController] pushViewController:dvc animated:YES]; 
} 

回答

0

這就是爲什麼viewDidLoad僅在第一次加載視圖時被調用;爲了解決您的問題,您必須將您執行該操作的操作推遲到方法viewWillAppear

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated] 
    if (startButton.hidden == NO){ 
     [pauseButton setHidden:YES]; 
     [continueButton setHidden:YES]; 
    } else { 
     [pauseButton setHidden:NO]; 
     [continueButton setHidden:NO]; 
    } 
} 

編輯

好吧,你的問題是由於您的DetailViewController小號獲取每個它們被推送到導航堆棧的頂部時釋放的事實,這就是爲什麼你看到他們在控制視圖與第一次推送時相同的狀態。

// here the detail view controller gets allocated 
DetailViewController *dvc = [[DetailViewController alloc]init]; 

我不明白你的任務追蹤系統的預期行爲:特別的方式來確定當給定的任務可以視爲完成 - 即你的倒計時邏輯適用於每個任務的方式(這是什麼應該發生,例如當你開始一個任務,突然你的應用程序進入後臺,計時器停止,然後恢復應用程序回到前臺?等)

假設您希望您的計時器恢復其狀態爲你推回同樣的看法,你推前幾秒,

我建議你使用一個刪除門(或者如果你願意的話),例如

- (void)timeElapsed:(NSTimeInterval)secondsElapsed forTask:(NSUInteger)task 
{ 
    //here you can save, e.g., in your task array the remaining seconds the task have to run 
    // for example, supposing you're using a linear index to track down the position of the associated timer 
    NSTimeInterval remainingSeconds = [self.taskArray[task] timeInterval] - secondsElapsed; 
    [self.taskArray[task] setTimeInterval:remainingSeconds]; 
} 

表視圖控制器應實現該方法,並且DetailViewController應與相關的相關聯的任務的索引進行初始化。

您還應該對DetailViewController進行以下更改,以向tableViewController發送給定任務所消耗的秒數。

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    [self.timer invalidate]; 
    self.timer = nil; 
    // remember to check if the delegate actually performs that selector 
    [delegate timeElapsed:secondsElapsed forTask:taskIndex]; 
} 
+0

沒有工作......當我回去重新點擊單元格時,啓動按鈕就在那裏,而另一個按鈕則無法工作。此外,標籤爲空(00:00:00)..原始帖子更新了更新代碼 – EvilAegis

+0

你的代碼在tableview委託方法中做了什麼?請發佈 – HepaKKes

+0

我已更新原始問題 – EvilAegis

相關問題