如何在内存中从一个视图控制器访问自定义对象?(How do I access a custom ob

2019-10-17 17:15发布

在我的应用我去取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控制器?

Answer 1:

当你正在使用的核心数据,最好的选择是使用NSFetchedResultsController来填充表视图。

所有你需要的则是管理对象背景下,无论是从另一个控制器或应用程序的委托。



文章来源: How do I access a custom object in memory from a view controller?