在我的应用程序中,我获取一个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控制器?
使用正确的标签。你认为对象标签是获得IOS开发答案的适当标签吗? – mpm 2013-02-14 16:32:57
我很难理解你的问题。你在问如何根据距离对你的'self.stores'对象进行排序吗? – 2013-02-14 20:31:10
我有一个mapview控制器和一个tableview控制器。 mapviewcontroller很好。我希望tableview单元格按距离排序。目前,这些单元格是从self.stores数组中的Store对象填充的。存储对象显然没有距离(userLocation)属性。因此,在mapviewcontroller中,我使用plotStores方法,将db提取放入self.stores数组中,并通过使用MyLocation NSObject(它具有distance属性)循环来创建MKannotations。有没有办法只是访问这些MyLocation对象来排序单元格? – marciokoko 2013-02-14 20:43:22