viewForAnnotation中的EXC_BAD_ACCESS

用户名

我有以下代码为我提供了EXC_BAD_ACCESS。

我有一张地图,正在加载一个圆圈图像,该图像表示地图中的实际位置。

在这里编辑完整代码

#import "ComoLlegarViewController.h"
#import "MBProgressHUD.h"

/*#define COLOR_TEXTO_NORMAL          [UIColor colorWithRed:204.0f/255.0f green:204.0f/255.0f blue:204.0f/255.0f alpha:1.0f]*/
#define COLOR_TEXTO_NORMAL [UIColor darkGrayColor]
#define COLOR_TEXTO_SELECCIONADO    [UIColor colorWithRed:0.0f/255.0f green:10.0f/255.0f blue:100.0f/255.0f alpha:1.0f]


#define kTIENDA_UNAM_LATITUD    19.321066
#define kTIENDA_UNAM_LONGITUD   -99.176202

@interface ComoLlegarViewController ()

@property (strong, nonatomic) MKPointAnnotation *puntoTiendaUNAM;

// Metodo que crea un punto en el Mapa y apunta a la Tienda UNAM
- (void)dropPinUbicacionTiendaUNAM;
- (void)hacerZoomAlMapa;
@end


@implementation ComoLlegarViewController

@synthesize segmentedControl = _segmentedControl;
@synthesize mapa = _mapa;
@synthesize puntoTiendaUNAM = _puntoTiendaUNAM;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self) {

        [self.navigationController.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:COLOR_TEXTO_NORMAL, NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

        [self.navigationController.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:COLOR_TEXTO_SELECCIONADO, NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];


        [self.navigationController.tabBarItem setSelectedImage:[[UIImage imageNamed:@"icono_mapas_activado.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    }
    return self;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName,
                                [UIColor grayColor], NSForegroundColorAttributeName,
                                [NSNumber numberWithFloat:0.1f], NSShadowAttributeName,
                                nil];

    [_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];

    // No need to retain (just a local variable)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Localizando";
    hud.detailsLabelText = @"Tienda UNAM";
    [hud hide:YES afterDelay:0.7f];

    // localizamos el pin en el mapa en el background
    [self dropPinUbicacionTiendaUNAM];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    _mapa = nil;
    _segmentedControl = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


/**
 Metodo que se encarga de mostrar el pin en el mapa con la ubicacion de la tienda UNAM
 */

- (void)dropPinUbicacionTiendaUNAM
{    
    // Referencia al mapa de la clase
    MKMapView *myMapView = _mapa;

    // Creamos un pin
    if (! _puntoTiendaUNAM) {
        _puntoTiendaUNAM= [[MKPointAnnotation alloc] init];
    }

    _puntoTiendaUNAM.title = @"TU Tienda UNAM";
    _puntoTiendaUNAM.coordinate = CLLocationCoordinate2DMake(kTIENDA_UNAM_LATITUD, kTIENDA_UNAM_LONGITUD);
    _puntoTiendaUNAM.subtitle = @"Dalias s/n, Oxtopulco, 04510 Coyoacán";

    // Lo agregamos al mapa
    [myMapView addAnnotation:_puntoTiendaUNAM];
    [myMapView setCenterCoordinate:_puntoTiendaUNAM.coordinate animated:YES];

    // Zoom al mapa para mostrar solo la region donde esta el pin, con un span de 1000 mts de radio
    MKCoordinateRegion mapRegion;
    mapRegion.center = _puntoTiendaUNAM.coordinate;
    mapRegion.span = MKCoordinateSpanMake(0.01, 0.01);
    [myMapView setRegion:mapRegion animated: YES];
}

#pragma mark MKMapView Delegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // Creamos la vista de punto a desplegar
    MKPinAnnotationView *pinView = nil;
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinTiendaUNAM"];

    if (! pinView) {

        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinTiendaUNAM"];

        // Configuramos la vista del punto

        UIImage *img=[UIImage imageNamed:@"bolita_mapa.png"];

        UIImageView *pinImageView = [[UIImageView alloc] initWithImage:img];
        [pinImageView setUserInteractionEnabled:YES];

        // Agregamos un gesture Recognizer a la bolita
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hacerZoomAlMapa)];
        [tapGestureRecognizer setNumberOfTapsRequired:1];
        [tapGestureRecognizer setNumberOfTouchesRequired:1];
        [pinImageView addGestureRecognizer:tapGestureRecognizer];

        [pinView addSubview:pinImageView];
        [pinView setPinColor:MKPinAnnotationColorPurple];
        [pinView setAnimatesDrop:YES];
        [pinView setCanShowCallout:YES];
        [pinView setCalloutOffset:CGPointMake(-8.0f, 0.0f)];
        // [pinView setSelected:YES animated:YES];

        UIButton *botonBrujula = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        //UIButton *botonBrujula = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];        
        //[botonBrujula setImage:[UIImage imageNamed:@"icono_mapa.png"] forState:UIControlStateNormal];
        //[botonBrujula addTarget:self action:@selector(hacerZoomAlMapa) forControlEvents:UIControlEventTouchUpInside];
        // [botonBrujula addTarget:self action:@selector(verInfo) forControlEvents:UIControlEventTouchUpInside];
        [pinView setRightCalloutAccessoryView:botonBrujula];


        // Agregamos el logo de tienda unam a la vista del punto
        UIImage *image = [UIImage imageNamed:@"logo_tienda_unam.png"];
//        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        UIButton *botonLogoTiendaUNAM = [UIButton buttonWithType:UIButtonTypeCustom];
        [botonLogoTiendaUNAM setFrame:CGRectMake(0, 0, 30, 30)];
        [botonLogoTiendaUNAM setBackgroundImage:image forState:UIControlStateNormal];

//        // Agregamos un gesture Recognizer al logo de tienda unam
//        UITapGestureRecognizer *tapGestureRecognizer_logo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hacerZoomAlMapa)];
//        [tapGestureRecognizer_logo setNumberOfTapsRequired:1];
//        [tapGestureRecognizer_logo setNumberOfTouchesRequired:1];
//        [imageView addGestureRecognizer:tapGestureRecognizer_logo];

//        [pinView setLeftCalloutAccessoryView:imageView];
        [pinView setLeftCalloutAccessoryView:botonLogoTiendaUNAM];


 //Problema con la vista , entra en recursividad y se desborda el programa , corregir la annotation

        // Seleccionamos el pin para mostrar la informacion de 
    // [mapView selectAnnotation:annotation animated:YES];

        // Establecemos un tag a los callout views para diferenciarlos
        [[pinView rightCalloutAccessoryView] setTag:1];     // Ver Info
        [[pinView leftCalloutAccessoryView] setTag:2];      // Hacer Zoom

    } else {

        pinView.annotation = annotation;

    }


    pinView.annotation=annotation;
    return pinView;
}





- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{

[mapView selectAnnotation:self.puntoTiendaUNAM animated:YES];


}





- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    if ([control tag] == 1) {
        [self performSegueWithIdentifier:@"ver_info" sender:self];

    } else if ([control tag] == 2) {
        [self hacerZoomAlMapa];
    }
}


- (void)hacerZoomAlMapa
{
    [_mapa deselectAnnotation:_puntoTiendaUNAM animated:NO];

    // Zoom al mapa para mostrar solo la region donde esta el pin, con un span de 1000 mts de radio
    MKCoordinateRegion mapRegion;
    mapRegion.center = _puntoTiendaUNAM.coordinate;
    mapRegion.span = MKCoordinateSpanMake(0.01, 0.01);
    [_mapa setRegion:mapRegion animated:YES];

    [_mapa selectAnnotation:_puntoTiendaUNAM animated:NO];
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    // Volvemos a mostrar el callout, por default es 'animated = YES'
    [_mapa performSelector:@selector(selectAnnotation:animated:) withObject:_puntoTiendaUNAM afterDelay:0.5f];
}


- (IBAction)seleccionarTipoMapa:(id)sender
{
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    NSInteger indiceSeleccionado = [segmentedControl selectedSegmentIndex];

    switch (indiceSeleccionado) {

        case 0:     // Mapa tipo Normal
            [_mapa setMapType:MKMapTypeStandard];
            break;

        case 1:     // Mapa tipo Hibrido
            [_mapa setMapType:MKMapTypeHybrid];
            break;

        case 2:     // Mapa tipo Satelite
            [_mapa setMapType:MKMapTypeSatellite];
            break;

        default:
            break;
    }
}

@end

编辑与安娜的建议代码是

用户名

此行导致EXC_BAD_ACCESS:

[mapView selectAnnotation:annotation animated:YES];

您不应尝试从viewForAnnotation委托方法本身中选择注释

selectAnnotation:animated:viewForAnnotation方法中删除对的调用


selectAnnotation:animated:被调用时,地图视图需要重绘注解视图,并显示其标注。重画视图导致viewForAnnotation被调用。因为在该方法中,代码正在调用selectAnnotation:animated:它,导致viewForAnnotation再次被调用,依此类推,导致递归用完内存后崩溃。

如果要在将注释的标注添加到地图后立即对其进行显示,请didAddAnnotationViews改为使用委托方法。有关此示例,请参见在iOS5中未选择MKAnnotation另请注意,一次只能选择一个注释(显示其标注)。



与问题无关,但不是像这样在附件视图上设置标签:

[[pinView rightCalloutAccessoryView] setTag:1];     // Ver Info
[[pinView leftCalloutAccessoryView] setTag:2];      // Hacer Zoom

在中calloutAccessoryControlTapped,您可以直接检查control 自身是否等于左右附件视图。请参见如何将地图注释视图按钮与数据库连接以转到另一个视图?举个例子

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从EXC_BAD_ACCESS中恢复?

来自分类Dev

在AVAssetReader中获取Exc_Bad_access

来自分类Dev

Swift中的UIWebView:exc_bad_access

来自分类Dev

Swift中的UIDocumentInteractionControllerDelegate EXC_BAD_ACCESS

来自分类Dev

fscanf中的Exc_bad_access错误

来自分类Dev

EXC_BAD_ACCESS与OSX中的wxWidgets

来自分类Dev

Swift中的UIWebView:exc_bad_access

来自分类Dev

在AVAssetReader中获取Exc_Bad_access

来自分类Dev

调试EXC_BAD_ACCESS

来自分类Dev

NSDictionary EXC_BAD_ACCESS

来自分类Dev

AFHTTPRequestOperationManager的EXC_BAD_ACCESS

来自分类Dev

EXC_BAD_ACCESS在mergeChangesFromContextDidSaveNotification

来自分类Dev

SKTexture EXC_BAD_ACCESS

来自分类Dev

EXC_BAD_ACCESS StringWithFormat

来自分类Dev

发布中的简单代码中的EXC_BAD_ACCESS

来自分类Dev

Xcode中C中的exc_bad_access代码1

来自分类Dev

自我阻止中的EXC_BAD_ACCESS

来自分类Dev

在NSUserDefaults中存储字典导致EXC_BAD_ACCESS

来自分类Dev

NSManagedObject在Swift中实现协议的EXC_BAD_ACCESS错误

来自分类Dev

在MKMapView中添加注释时EXC_BAD_ACCESS

来自分类Dev

Swift中的UIAlertView,获取EXC_BAD_ACCESS

来自分类Dev

Swift中的parser.parse()导致EXC_BAD_ACCESS

来自分类Dev

Swift中的泛型和EXC_BAD_ACCESS

来自分类Dev

实现KVO的类中的EXC_BAD_ACCESS

来自分类Dev

调试线程1:C中的EXC_BAD_ACCESS

来自分类Dev

GLEW 1.10:glewInit中的EXC_BAD_ACCESS

来自分类Dev

dismissViewControllerAnimated仅导致iOS 7.1中的EXC_BAD_ACCESS

来自分类Dev

在弧中消失的self.delegate:EXC_BAD_ACCESS

来自分类Dev

单例iOS中removeFromSuperview上的EXC_BAD_ACCESS