Ios MapView向自定义注释添加操作(右箭头)
我通过以下for循环创建注释接点:Ios MapView向自定义注释添加操作(右箭头),ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我通过以下for循环创建注释接点: UIButton *eventMore = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; for (i = 0; i < [results count]; i++) { //NSLog(@"Result: %i = %@", i, results[i]); //NSLog(@"%@",[[results objectAtIndex:i] objectForKey:@"lo
UIButton *eventMore = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
for (i = 0; i < [results count]; i++) {
//NSLog(@"Result: %i = %@", i, results[i]);
//NSLog(@"%@",[[results objectAtIndex:i] objectForKey:@"long"]);
myAnn = [[CustomAnnotation alloc] init];
location.latitude = (double)[[[results objectAtIndex:i] objectForKey:@"lat"] doubleValue];
location.longitude = (double)[[[results objectAtIndex:i] objectForKey:@"long"] doubleValue];
myAnn.coordinate = location;
myAnn.title = [[results objectAtIndex:i] objectForKey:@"title"];
myAnn.subtitle = [[results objectAtIndex:i] objectForKey:@"strap"];
[eventMore addTarget:myAnn action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[locations addObject:myAnn];
//NSLog(@"%i", [[results objectAtIndex:i] objectForKey:@"lat"]);
}
您没有为注释设置调用访问视图 示例代码
for (i = 0; i < [results count]; i++) {
MKPointAnnotation *myAnn = [[MKPointAnnotation alloc] init];
location.latitude = (double)[[[results objectAtIndex:i] objectForKey:@"lat"] doubleValue];
location.longitude = (double)[[[results objectAtIndex:i] objectForKey:@"long"] doubleValue];
myAnn.coordinate = location;
myAnn.title = [[results objectAtIndex:i] objectForKey:@"title"];
myAnn.subtitle = [[results objectAtIndex:i] objectForKey:@"strap"];
[locations addObject:myAnn];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *s = @"ann";
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
if (!pin) {
pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"pin.png"];
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self
action:@selector(viewDetails) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = button;
}
return pin;
}
for(i=0;i<[结果计数];i++){
MKPointAnnotation*myAnn=[[MKPointAnnotation alloc]init];
location.latitude=(double)[[results objectAtIndex:i]objectForKey:@“lat”]doubleValue];
location.longitude=(double)[[results objectAtIndex:i]objectForKey:@“long”]doubleValue];
myAnn.坐标=位置;
myAnn.title=[[results objectAtIndex:i]objectForKey:@“title”];
myAnn.subtitle=[[results objectAtIndex:i]objectForKey:@“strap”];
[对象:myAnn];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
静态NSString*s=@“ann”;
MKAnnotationView*引脚=[mapView dequeueReusableAnnotationViewWithIdentifier:s];
如果(!pin){
pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:s];
pin.canShowCallout=是;
pin.image=[UIImage ImageName:@“pin.png”];
pin.calloutOffset=CGPointMake(0,0);
UIButton*button=[UIButton button类型:UIButtonTypedTailButton];
[按钮添加目标:自我
操作:@selector(viewDetails)for ControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView=按钮;
}
回位销;
}
是否应该在for循环中创建Evermore按钮?我不知道按您的方式操作是否会导致按钮多次触发选择器。从mkannotationview派生CustomAnnotion annotation和annotation view是不同的。我的示例是MKMapViewDelegate方法,它为注释提供注释视图。您应该将该按钮添加到视图中。因此,请按原样实现该方法is@Anil,正如您正确地说的“Annotation和Annotation view是不同的…”,您的答案中的第一个示例代码行(myAnn.right…)令人困惑,因为它意味着myAnn既是id又是MKAnnotationView(不推荐)。我建议将其删除。是否检查是否调用委托方法?当你点击pin时会发生什么?@AnnaKarenina当然。。谢谢我最初得到的回答是,我认为myAnn是一个注释视图。
for (i = 0; i < [results count]; i++) {
MKPointAnnotation *myAnn = [[MKPointAnnotation alloc] init];
location.latitude = (double)[[[results objectAtIndex:i] objectForKey:@"lat"] doubleValue];
location.longitude = (double)[[[results objectAtIndex:i] objectForKey:@"long"] doubleValue];
myAnn.coordinate = location;
myAnn.title = [[results objectAtIndex:i] objectForKey:@"title"];
myAnn.subtitle = [[results objectAtIndex:i] objectForKey:@"strap"];
[locations addObject:myAnn];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *s = @"ann";
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
if (!pin) {
pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:@"pin.png"];
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self
action:@selector(viewDetails) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = button;
}
return pin;
}