iphone - 在导航 Controller 中按下后退按钮时是否可以不释放 View Controller ?

标签 iphone ios objective-c dealloc detailview

这是我的问题:我有一个包含一堆单元格的表格 View 。 Core Data 使用 NSFetchedResultsController 将 Task 对象加载到单元格中。现在我有了它,所以每个单元格都有一个 DetailViewController,它存储在字典中,如下所示:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *detailVC;
    if (![self.detailViewsDictionary.allKeys containsObject:@(indexPath.row)]){
        detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
        [self.detailViewsDictionary setObject:detailVC forKey:@(indexPath.row)];
    }else{
        detailVC = self.detailViewsDictionary[@(indexPath.row)];
    }
        Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        detailVC.testTask = task;
        [[self navigationController] pushViewController:detailVC animated:YES];
}

每个 DetailViewController 都有一个计时器属性,该属性从与 TableView 中任何给定索引路径处的单元格相关的任务对象获取其时间间隔。这是我的详细 View Controller 的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSThread detachNewThreadSelector:@selector(startTimer:) toTarget:self withObject:nil];
}
-(IBAction)startTimer:(id)sender{
//default value of showButtonValue when first loaded is 1
    if (testTask.showButtonValue == 1) {
        [startButton setTitle:@"Start" forState:UIControlStateNormal];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        testTask.showButtonValue = 3;
    } else if (testTask.showButtonValue == 2) {
        [startButton setTitle:@"Resume" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
        testTask.showButtonValue = 3;
    } else if (testTask.showButtonValue == 3){
        [startButton setTitle:@"Pause" forState:UIControlStateNormal];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        testTask.showButtonValue = 2;
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    nameTextField.text = testTask.taskName;
    [timerLabel setFont:[UIFont fontWithName:@"Arial" size:60]];
    NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
    NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
                        seconds / 3600, (seconds / 60) % 60, seconds % 60];
    timerLabel.text = string;
    [[self navigationItem] setTitle:[testTask taskName]];

    [timerLabel setNeedsDisplay];
    [self.view setNeedsDisplay];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    testTask.taskName = nameTextField.text;
}
-(void)timerAction:(NSTimer *)t
{
    if(testTask.timeInterval == 0)
    {
        if (self.timer)
        {
            [self timerExpired];
            [self.timer invalidate];
            self.timer = nil;
        }
    }
    else
    {
        testTask.timeInterval--;
    }
    NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
    NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
                        seconds / 3600, (seconds / 60) % 60, seconds % 60];
    timerLabel.text = string;
    NSLog(@"%f", testTask.timeInterval);
}

无论细节 View Controller 当前是否在屏幕上,计时器都应该继续运行。当我第一次点击一个单元格时,转到详细 View Controller 并启动计时器,然后返回到 TableView ,没有问题,倒计时继续进行。问题是,如果我重新点击单元格,则会创建另一个计时器,因此时间会以 2 倍的速度递减!不仅如此,显示剩余时间的字符串不会在 ViewWillAppear 时更新,它只会在第一次调用 timerAction 时更新。

有一堆问题,我真的很感激你的帮助!

最佳答案

您在 didSelectRowAtIndexPath 中的代码(我在对您的其他问题 (one timer per detail view controller) 的回答中提供)确实会阻止 Controller 在您按下后退按钮时被释放。它通过在字典中保留对详细 View Controller 的引用来实现这一点。我猜它对你不起作用,因为你忘了初始化字典,所以它是零。

关于iphone - 在导航 Controller 中按下后退按钮时是否可以不释放 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17962040/

相关文章:

iphone - 滚动超过列表的开头或结尾时,UIPickerView 使应用程序崩溃

ios - 核心数据提交通知

objective-c - Xcode 中蓝牙设备的设备名称

android - iOS 是否有相当于 Android 音轨的版本?

iphone - 按下 Home 时使用 CCGLView 的 EXC_BAD_ACCESS

ios - UITableView didSelectRowAtIndexPath 只能识别用两根手指点击

ios - 在 Swift 中使用动态键解析 JSON

ios - 滑动手势不改变 UITableViewCell

sqlite并发问题

objective-c - 使用 ARC 时重新分配属性