每个细节视图控制器一个计时器 [英] one timer per detail view controller

查看:47
本文介绍了每个细节视图控制器一个计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有表视图属性的视图控制器,以及一个通过导航栏连接到表视图中每个单元格的详细信息视图控制器.在详细视图中,控制器是一个倒数计时器,在用户创建任务时指定了一个间隔.我正在尝试使每个单元(或任务)具有唯一的详细信息视图控制器.我正在使用核心数据.这就是我现在拥有的:

I have a view controller with a table view property and a detail view controller connected to each cell in the tableview via the navigation bar. In the detail view controller is a countdown timer, with an interval specified when the user creates the task. I am trying to make it so each cell (or task) has a unique detail view controller. I am using core data. This is what I have now:

-(void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (!self.detailViewController) {
        self.detailViewController = 
            [[DetailViewController alloc] initWithNibName:@"DetailViewController" 
                                                   bundle:nil];
    }
        Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        self.detailViewController.testTask = task;
        [[self navigationController] pushViewController:self.detailViewController 
                                               animated:YES];

}

DetailViewController.m

@interface DetailViewController : UIViewController <UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *timeLeft;
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@property (nonatomic, strong) Tasks *testTask;
@end

我觉得这是实现细节视图控制器的正确方法,因为它可以最小化需要创建的内存量,但是它并不真正适合我的需求.当前,当用户点击一个单元格,然后再点击并点击另一个单元格时,只会加载第一个单元格的属性.另外,如果我要删除一个单元格,则无法使用此方法来使它的计时器无效(我认为).有什么建议吗?

I feel like this is the correct way to implement the detail view controller because it minimizes the amount of memory that needs to be created, however it doesn't really suit my needs. Currently when a user taps a cell, and taps back, and taps a different cell, only the first cell's properties are loaded. Also, if I were to delete a cell there would be no way to invalidate its timer (i think) with this method. Any suggestions?

-编辑-我猜我应该问的问题是:应该如何使每个明细视图都有一个递减的标签(从计时器获取其信息)?

---edit--- I guess the question I should be asking is: How should I make it so that each Detail View has a decrementing label (that gets its information from a timer)?

推荐答案

您的代码的麻烦在于,您仅创建了一个DetailViewController实例,因此每个单元格都推送到同一个实例.您必须在didSelectRowAtIndexPath中使用某种方法来查看索引路径,并使用该方法确定要去到DetailViewController的哪个实例.一种方法是创建一个可变字典,以保存对DetailViewController实例的引用.您可以将键设置为与indexPath.row对应的NSNumber,其值将是DetailViewController的实例.因此,您的代码可能看起来像这样:

The trouble with your code is that you're only creating one instance of DetailViewController, so each cell is pushing to the same one. You have to have some way in didSelectRowAtIndexPath to look at the index path and use that to determine which instance of DetailViewController to go to. One way to do that would be to create a mutable dictionary to hold references to the instances of DetailViewController. You could have the keys be NSNumbers that correspond to the indexPath.row, and the value would be an instance of DetailViewController. So, your code might look something like this:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

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

}

detailControllersDict是指向NSMutableDictionary的属性.

detailControllersDict is property pointing to an NSMutableDictionary.

这篇关于每个细节视图控制器一个计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆