单击地图引脚以获取详细视图,即可获得EXC_BAD_ACCESS

时间:2014-07-23 07:48:58

标签: objective-c mkmapview exc-bad-access

我已经像这样实施了calloutAccessoryControlTapped

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation.title);
    if([_delegate respondsToSelector:@selector(mapView:didClickPinButton:)])
    {
        NSString *titleStr =  [(DisplayMap *)view.annotation title];
        [_delegate performSelector:@selector(mapView:didClickPinButton:)withObject:titleStr];
    }
}

当我点击pin的按钮时,它会转到主视图控制器上的方法mapView:didClickPinButton并显示错误:

  

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x ......)

这是mapView:didClickPinButton:方法:

- (void) mapView: (MapView *) mapView didClickPinButton:(NSString *) title
{
    NSLog(@"bsn name-->%@",title);
    for(int i=0;i<[self.categoryListArray count];i++)
    {
        NSMutableDictionary *couponDetail = [self.categoryListArray objectAtIndex:i];
        if([[couponDetail objectForKey:@"category_name"] isEqualToString:title])
        {
            NSNumber *coupon_type=[couponDetail objectForKey:@"coupon_type"];
            int c_type = [coupon_type intValue];
            if(c_type == 1)
            {
                [self callServiceForCouponTypeOneBlock:[couponDetail objectForKey:@"id"] ];
                break;
            }
            else if(c_type == 2)
            {
                BusinessViewController *businessVC = [[BusinessViewController       alloc]initWithNibName:@"BusinessViewController" bundle:nil];
                [businessVC setCouponListDetail:couponDetail];
                [self.navigationController pushViewController:businessVC animated:YES];
                break;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

[_delegate performSelector:@selector(mapView:didClickPinButton:)withObject:titleStr];

performSelector:withObject:方法只能用于调用 一个 对象参数的方法,但您的mapView:didClickPinButton:方法 两个 对象参数。


可以而是使用performSelector:withObject:withObject:,它允许您使用两个对象参数调用方法,如下所示:

[_delegate performSelector:@selector(mapView:didClickPinButton:) 
    withObject:mapView withObject:titleStr];


但是,在这里使用performSelector似乎并不是必需的。

直接在委托上调用方法会更容易也更简单,因此方法有多少参数无关紧要:

[_delegate mapView:mapView didClickPinButton:titleStr];