新的 PList 检索代码可防止调用 MKMapView 委托方法 (iOS)

发布于 2024-12-17 10:06:54 字数 2192 浏览 0 评论 0原文

我添加了一个 plist 数据库来存储 MKMapView 中的注释信息。一旦我实现了获取信息的代码,我的委托方法就不再被调用。

我添加的代码是:

- (void)viewDidLoad
{
    NSMutableArray *annotations = [[NSMutableArray alloc]init];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MillersStores" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *anns = [dict objectForKey:@"Root"];
    for(int i = 0; i < [anns count]; i++) {
        float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
        float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];

        MillersLocations *myAnnotation = [[MillersLocations alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
        myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Address"];
        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
        [myAnnotation release];
    }
}

这是不再被调用的委托方法之一是:

- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKPinAnnotationView *pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;

    UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MillersAnnotation.png"]];
    pinView.leftCalloutAccessoryView = leftIconView;

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = rightButton;
    return pinView;
}

我在那里有另一个委托方法,它也放大到也没有被调用的用户位置,以及不再被调用的 calloutAccessoryControlTapped 方法叫。

我知道它与新代码有关,但我对如何调试它感到困惑,因为它没有给我错误,而且我无法记录它,因为甚至没有调用整个方法。当我摆脱新代码时,旧代码可以正常工作......新代码中的什么内容否定了旧代码?

I added a plist database to store information for annotations in a MKMapView. Once I implemented the code to grab the information, my delegate methods are no longer being called.

The code I added was:

- (void)viewDidLoad
{
    NSMutableArray *annotations = [[NSMutableArray alloc]init];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MillersStores" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *anns = [dict objectForKey:@"Root"];
    for(int i = 0; i < [anns count]; i++) {
        float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
        float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];

        MillersLocations *myAnnotation = [[MillersLocations alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
        myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Address"];
        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
        [myAnnotation release];
    }
}

And this is one of delegate method that's no longer being called is:

- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKPinAnnotationView *pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;

    UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MillersAnnotation.png"]];
    pinView.leftCalloutAccessoryView = leftIconView;

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = rightButton;
    return pinView;
}

I have another delegate method in there that also zooms into the User's Location that's not being called either, as well as the calloutAccessoryControlTapped method that's no longer being called.

I know it has something to do with the new code, but I'm confused as to how to even debug this because it's not giving me errors and I can't log it because the entire methods aren't even being called. When I get rid of the new code, the old code works fine...What is it in the new code that negates the old code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2024-12-24 10:06:54

听起来地图视图的 delegate 属性未设置。

旧代码是否包含此行:

mapView.delegate = self;

将其添加到 viewDidLoad 中,或者在 IB 中,将地图视图的 delegate 出口连接到文件的所有者。

It sounds like the map view's delegate property is not set.

Did the old code contain this line:

mapView.delegate = self;

Add that to viewDidLoad or, in IB, connect the map view's delegate outlet to File's Owner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文