如何从视图控制器访问内存中的自定义对象?

时间:2013-02-14 16:29:13

标签: ios custom-object

在我的应用程序中,我获取了一个coredatabase并将结果放入一个名为self.stores的数组中。我将这些商店位置转换为具有距离属性的MyLocation对象。我像这样绘制MyLocation对象:

- (void)plotStorePositions:(NSString *)responseString {

    for (id<MKAnnotation> annotation in _mapView.annotations) {
        [_mapView removeAnnotation:annotation];
    }
    //CYCLE THRU STORE OBJECTS
    for (Store * storeObject in self.stores) {

        NSString * latitude = storeObject.latitude;
        NSString * longitude = storeObject.longitude;
        NSString * storeDescription = storeObject.name;
        NSString * address = storeObject.address;

        //CREATE MYLOCATION OBJECTS
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = latitude.doubleValue;
        coordinate.longitude = longitude.doubleValue;
        MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0];

        //CREATE A PIN FOR EACH MYLOCATION ANNOTATION
        CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];

        //SET USERLOCATION (Must move this code out)
        self.userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];

        //SET USERLOCATION TO APPDELEGATE (Must move this code out)
        AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
        myDelegate.userLocation = self.userLocation;

        //CALCULATE DISTANCE AND SET ITS THAT MYLOCATION's DISTANCE PROPERTY
        CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:userLocation];
        annotation.distance = calculatedDistance/1000;        

        [_mapView addAnnotation:annotation];

    }

}

这是在mapview中。然后我有一个tableview,我获取相同的coredatabase并再次获取self.stores。然后我遍历那个self.stores数组来创建STORE storeObjects以放入我的tableview单元格。但我想对那些细胞进行排序。如何获取应该在内存中的MyLocation对象?我是否需要将它们传递给appDelegate或tableview控制器?

1 个答案:

答案 0 :(得分:0)

在使用Core Data时,最好的选择是使用NSFetchedResultsController填充表格视图。

然后,您需要的是来自其他控制器或应用程序委托的托管对象上下文。