Objective c 如何在MKAnnotation中发送对象?
我想在用户点击pin时发送对象。我只知道如何发送标题,但这是不够的,因为有些标题是相同的 这是我的代码:Objective c 如何在MKAnnotation中发送对象?,objective-c,xcode,mkannotation,Objective C,Xcode,Mkannotation,我想在用户点击pin时发送对象。我只知道如何发送标题,但这是不够的,因为有些标题是相同的 这是我的代码: ... - (void)viewDidLoad { [super viewDidLoad]; if (context == nil) { context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; }
...
- (void)viewDidLoad {
[super viewDidLoad];
if (context == nil)
{
context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
/* ********************************************** */
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Offers" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//[request setFetchBatchSize:20];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subOffer == %@", self.subItem];
[request setPredicate:predicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"offerOrder" ascending:YES];
NSArray *newArray = [NSArray arrayWithObject:sort];
[request setSortDescriptors:newArray];
NSError *error;
NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];
//NSLog(@"%@", results);
/* **************************************************** */
mapView.delegate=self;
NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = 43.82078;
theCoordinate1.longitude = 15.307265;
for (Offers *offer in results){
CLLocationCoordinate2D theCoordinate1;
MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];
theCoordinate1.latitude = [offer.lat doubleValue];
theCoordinate1.longitude = [offer.lon doubleValue];
myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title = offer.offerName;
myAnnotation1.subtitle = offer.offerLocation;
[mapView addAnnotation:myAnnotation1];
[annotations addObject:myAnnotation1];
}//end for
//NSLog(@"%d",[annotations count]);
//[self gotoLocation];//to catch perticular area on screen
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Walk the list of overlays and annotations and create a MKMapRect that
// bounds all of them and store it into flyTo.
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
//NSLog(@"fly to on");
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
//NSLog(@"else-%@",annotationPoint.x);
//NSLog(@"else");
}
}
// Position the map so that all overlays and annotations are visible on screen.
mapView.visibleMapRect = flyTo;
UIBarButtonItem* temp=[[UIBarButtonItem alloc] init];
temp.title=@"Back";
self.navigationItem.backBarButtonItem=temp;
//[temp release];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorRed;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
-(IBAction)showDetails:(id)sender{
//NSLog(@"Annotation Click");
DetailViewController *detail = [[DetailViewController alloc] init];
detail.title = ((UIButton*)sender).currentTitle;
[self.navigationController pushViewController:detail animated:YES];
}
...
。。。
-(无效)viewDidLoad{
[超级视图下载];
如果(上下文==nil)
{
上下文=[(AppDelegate*)[[UIApplication sharedApplication]委托]managedObjectContext];
}
/* ********************************************** */
NSEntityDescription*实体=[NSEntityDescription entityForName:@“提供”在托管对象上下文:上下文中];
NSFetchRequest*request=[[NSFetchRequest alloc]init];
//[请求setFetchBatchSize:20];
[请求集合实体:实体];
NSPredicate*谓词=[NSPredicate谓词格式:@“子供应商==%@”,self.subItem];
[请求集谓词:谓词];
NSSortDescriptor*排序=[[NSSortDescriptor alloc]initWithKey:@“Offerder”升序:是];
NSArray*newArray=[NSArray arrayWithObject:sort];
[请求集合集合描述符:newArray];
n错误*错误;
NSMutableArray*结果=[[context executeFetchRequest:请求错误:&错误]mutableCopy];
//NSLog(@“%@”,结果);
/* **************************************************** */
mapView.delegate=self;
NSMutableArray*注释=[[NSMutableArray alloc]init];
CLLocation Coordination 2和Coordination 1;
坐标1.纬度=43.82078;
坐标1.经度=15.307265;
针对(报价*结果报价){
CLLocation Coordination 2和Coordination 1;
MyAnnotation*myAnnotation1=[[MyAnnotation alloc]init];
坐标1.latitude=[offer.lat doubleValue];
坐标1.longitude=[offer.lon doubleValue];
myAnnotation1.坐标=坐标1;
myAnnotation1.title=offer.offerName;
myAnnotation1.subtitle=offer.offerLocation;
[地图视图添加注释:myAnnotation1];
[注释添加对象:myAnnotation1];
}//结束
//NSLog(@“%d”,[annotations count]);
//[自定位];//捕捉屏幕上的特定区域
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem;
//浏览覆盖图和注释列表,并创建一个
//将它们绑定并存储到flyTo中。
MKMapRect flyTo=MKMapRectNull;
用于(注释中的id注释){
//NSLog(@“飞到on”);
MKMapPoint annotationPoint=MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect=MKMapRectMake(annotationPoint.x,annotationPoint.y,0,0);
if(MKMapRectIsNull(flyTo)){
flyTo=pointRect;
}否则{
flyTo=MKMapRectUnion(flyTo,pointRect);
//NSLog(@“else-%@”,annotationPoint.x);
//NSLog(@“else”);
}
}
//定位地图,使所有覆盖和注释在屏幕上可见。
mapView.visibleMapRect=flyTo;
uibarbuttoneim*temp=[[uibarbuttoneim alloc]init];
临时标题=@“返回”;
self.navigationItem.backBarButtonim=temp;
//[临时释放];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
//如果是用户位置,只需返回nil。
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
//首先尝试将现有pin视图出列
静态NSString*AnnotationIdentifier=@“AnnotationIdentifier”;
MKPinAnnotationView*pinView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation重用标识符:AnnotationIdentifier];
pinView.animatesDrop=是;
pinView.canShowCallout=是;
pinView.pinColor=MKPinAnnotationColorRed;
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
[rightButton setTitle:annotation.title for状态:UIControlStateNormal];
[rightButton添加目标:自我
操作:@选择器(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView=rightButton;
返回pinView;
}
-(iAction)显示详细信息:(id)发件人{
//NSLog(@“注释点击”);
DetailViewController*detail=[[DetailViewController alloc]init];
detail.title=((UIButton*)发送者).currentTitle;
[self.navigationController pushViewController:详细动画:是];
}
...
至少我不完全理解你的意思。你能说得更具体一点,确切地指出你想在哪里做什么吗?谢谢。当用户点击pin时,我想在IBAction“showDetails”中发送提供对象。好的,但您从哪里获得此提供对象?你是怎么储存的?您只想传递一个报价对象吗?在“viewDidLoad”中,我存储报价。您将看到以下代码:…for(Offers*offer in results){…首先:我必须假设结果是一个ivar。在这种情况下,从技术上讲,您并没有存储任何内容。您只是在枚举这些offer对象。您想传递所有这些对象吗?还是只传递一个特定的对象?您可以基于什么基础来决定传递哪个对象?