0

我正在尝试使用从更改UIDatepicker. 目前我有以下方法可以捕获数据并将其发送到UIButton(有效)。我正在尝试重置包含 UITableViewController 的容器视图,该视图被调用DisplayTableViewController但它没有重新加载NSFetchController. 有关如何调试或解决此问题的任何建议?

-(void)changeDate:(UIDatePicker *)sender {

    //NSDate Formatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"ddMMyyyy"];

    //Convert to string
    NSString *stringFromDate = [dateFormatter stringFromDate:sender.date];

    //Send to UIButton
    [dateLabel setTitle:stringFromDate forState:UIControlStateNormal];

    DisplayTableViewController *tbc = (DisplayTableViewController *)self.childViewControllers[0];
    [tbc.tableView reloadData];

}

在此处输入图像描述


编辑

 if ([[segue identifier] isEqualToString:@"ShowDisplayResults"])
    {
        //Format Date for Seague
        NSDateFormatter *todaysdate = [[NSDateFormatter alloc] init];
        [todaysdate setDateFormat:@"ddMMyyyy"];
        //Convert to string for Seague
        NSString *stringFromDate = [todaysdate stringFromDate:[NSDate date]];
        // Store the text we entered to dateSeague
        dateSeague =stringFromDate;
        // Get reference to the destination view controller
        DisplayTableViewController *targetVC = [segue destinationViewController];
        // Pass any objects to the view controller here, like...
        targetVC.dateSeague = dateSeague;
        targetVC.Ex = self.Ex;

    }

容器中的 NSFETCHCONTROLLER

- (NSFetchedResultsController *)fetchedResultsController {
    NSLog(@"Test Date for second seague = %@", dateSeague);
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    //Query Entity
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WorkoutDetails"inManagedObjectContext:managedObjectContext];
    //Fetch Entity
    [fetchRequest setEntity:entity];

    //Sort Returned Values
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"weight" ascending:NO];
    //Fetch Sorted Array
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    //Set Return Size
    [fetchRequest setFetchBatchSize:20];

    //Predicate Results Based on ID
    NSPredicate *sortName = [NSPredicate predicateWithFormat:@"exid == %@", Ex.name];
    //Get NSDate format from String
    NSString *stringFromDate = dateSeague;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    //Convert Back to NSDate
    [df setDateFormat:@"ddMMyyyy"];
    NSDate *inputedDate = [df dateFromString: stringFromDate];
    //Set Predicate
    NSPredicate *sortDate = [NSPredicate predicateWithFormat:@"date == %@", inputedDate];
    //Compound Predicate Array
    NSPredicate *placesPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:sortName, sortDate, nil]];
    //Fetch Predicate Compound Array
    [fetchRequest setPredicate:placesPredicate];
    //Continue Fetching!
    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext sectionNameKeyPath:nil
                                                   cacheName: nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}
4

2 回答 2

0

首先self.childViewControllers[0]将为您提供视图控制器层次结构中的第一个视图控制器,并且只有当您有一个包含另一个视图控制器的视图控制器时才需要这个。

其次,使用这种方法不是一个好习惯,因为当您调用它时您并不真正知道视图控制器的状态,并且该索引处的视图控制器可能与您预期的不同。

因此,我建议您为您的视图控制器或包含日期选择器的视图实现一个委托,当日期更改并且您对日期执行所有操作时,请调用委托。如果您不想对日期做任何事情,只想将它传递给视图控制器,那么您可以将日期选择器delegate设置为您的DisplayTableViewController

作为旁注,我不知道您现在拥有的层次结构,如果您认为我的回答没有帮助,请在您的问题中添加更多详细信息(您想要获得的层次结构和流程)

于 2013-07-12T21:24:09.567 回答
0

查询参数是否NSFetchRequestNSFetchController变化?如果是这样,请确保在使用缓存时使用 deleteCacheWithName,然后重新配置 NSFetchController。

于 2013-07-12T21:25:10.933 回答