需要从DidSelectRow获取NSObject

时间:2011-06-26 09:58:14

标签: iphone objective-c nsfetchedresultscontroller

我的视图中有一个加载了以下提取代码的日历:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Object" inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];
    [request setResultType:NSDictionaryResultType];
    [request setReturnsDistinctResults:YES];
    [request setPropertiesToFetch :[NSArray arrayWithObject:@"timeStamp"]];

    NSError *error;
    NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
    NSArray *tempData = [objects valueForKey:@"timeStamp"];

    NSMutableArray * data1 = [NSMutableArray array];
    NSCalendar * gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSUInteger flags = ( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit );
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss Z"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

    [tempData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSDate * date = [gregorian dateFromComponents:[gregorian components:flags fromDate:obj]];
        [data1 addObject:[formatter stringFromDate:date]];
    }];
    self.data = data1;

当我在日历中选择日期时,我有以下代码将推送到下一个详细视图

- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d { 
// Assuming `data` is from the code above & the date is in `00:00:00 +0000` form.

NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

NSString * dateString = [formatter stringFromDate:d];

if ( [data containsObject:dateString] ) {
    // Push object to detail view??
}

}

我的问题是,如何获取已取回的对象,其中timeStamp属性是选定的日期?我想将Object推送到下一个视图,其中timeStamp属性是我从日历中选择的日期,该日期是从我上面发布的获取请求数组中加载的。

1 个答案:

答案 0 :(得分:0)

您需要使用NSPredicate实例来获取特定数据。您需要在上面的if子句

中添加它
if ( [data containsObject:dateString] ) {
    NSDate * searchDate = [formatter dateFromString:dateString];
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@" timeStamp == %@ ", searchDate];

    /* Create a fetch request with the predicate above and get the results from the MOC */
}