Note that there are some explanatory texts on larger screens.

plurals
  1. POsaving properties between views in navigation controller?
    text
    copied!<p>i have a navigation controller with a root view controller that is a table view controller. when you click on an individual cell, there is a timer (with label counting down) and start, pause, and continue button. when the view is loaded, the pause and continue button are hidden. when you click the start button, it should disappear and show the pause and continue button, and also the timer will start counting down. for some reason, when i click back on the navigation controller, and re-tap the same cell, none of the properties are loaded (label does not have a countdown time, and the start button is still there even though it should be hidden, and the pause/continue buttons are not there, even though they SHOULD be there). How would I make it so tapping a cell, going back, and re-tapping it show the same properties? (i dont think delegation is correct because its not a modal VC)</p> <p>Here is my code:</p> <p>propertiesviewcontroller.m (what shows up after you click on a cell)</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; [_timeLeft setFont:[UIFont fontWithName:@"BebasNeue" size:25]]; //if (!([components hour] &amp;&amp; [components minute] &amp;&amp; [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]; } </code></pre> <p>--EDIT-- ViewWillAppear update</p> <pre><code>-(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]; } </code></pre> <p>--Edit 2-- TableViewController.m</p> <pre><code>-(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]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload