在导航控制器中保存视图之间的属性

时间:2013-07-07 21:48:27

标签: iphone ios objective-c uitableview nstimer

我有一个带有根视图控制器的导航控制器,它是一个表视图控制器。当您单击单个单元格时,会有一个计时器(标签倒计时)以及启动,暂停和继续按钮。加载视图时,隐藏暂停和继续按钮。当您单击开始按钮时,它应该消失并显示暂停和继续按钮,并且计时器也将开始倒计时。出于某种原因,当我单击导航控制器并重新点击相同的单元格时,没有加载任何属性(标签没有倒计时时间,并且即使应该隐藏它,开始按钮仍然存在,并且暂停/继续按钮不存在,即使它们应该在那里)。如何点击一个单元格,返回并重新点击它显示相同的属性? (我不认为代表团是正确的,因为它不是模态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];
}

- 编辑 - 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];
}

1 个答案:

答案 0 :(得分: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];
}