0

plotParkingLotsアプリの起動時に最初に呼び出されるように (そして、後で実装する x 秒ごとに)、AppDelegate から呼び出そうとしています。

plotParkingLotsMapViewControllerviewDidLoadから呼び出すと正常に動作しますが、AppDelegate から呼び出すと動作しません。

呼び出されていることはわかっていますが、マップ ビューに切り替えると、注釈が表示されません。

MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

#define METERS_PER_MILE 1609.344

@interface MapViewController : UIViewController <MKMapViewDelegate>

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

- (void)plotParkingLots;

@end

MapViewController.m

- (void)plotParkingLots {
    NSString *url = [NSString stringWithFormat:@"http:/localhost/testes/parking.json"];
    NSData *jsonData = [NSData dataWithContentsOfURL: [NSURL URLWithString:url]];

    if (jsonData == nil) {
        UIAlertView *alertBox = [[UIAlertView alloc] initWithTitle: @"Erro de conexão" message: @"Não foi possível retornar os dados dos estacionamentos" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
        [alertBox show];
    }
    else {
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

        NSArray *parkings = [json objectForKey:@"parkings"];
        for (NSDictionary * p in parkings) {
            Parking *annotation = [[Parking alloc] initWithDictionary:p];
            [_mapView addAnnotation:annotation];
        }
    }
 }

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    static NSString *identifier = @"Parking";
    if ([annotation isKindOfClass:[Parking class]]) {
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        } else {
            annotationView.annotation = annotation;
        }

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.image=[UIImage imageNamed:@"car.png"];

        return annotationView;
    }

    return nil;    
}

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[MapViewController alloc] plotParkingLots];

    return YES;
}
4

1 に答える 1

1

ユーザーに表示される MapViewController と話しているわけではありません。AppDelegate では、MapViewController を一時的に割り当て、駐車場をプロットしてから、その MapViewController を破棄し、二度と使用しません。

ユーザーが何かを押してそのビューコントローラーを表示させたときに、駐車場をプロットしたばかりのものと同じになるように、そのコントローラーへのハンドルを保持する必要があります。

于 2012-09-27T20:57:03.390 に答える