0

我从 tableViewController 开始,一旦我点击一个单元格,它就会弹出一个 mapView,它执行我在 MapViewController 中声明的方法:

MapViewController *category1 = [[MapViewController alloc] init];
     NSArray *array=[self.navigationController viewControllers];
[category1 categoryDining];
            [self.navigationController popToViewController:[array objectAtIndex:[array count]-2] animated:YES];

这工作得很好。它弹出到 mapViewController,并执行以下方法:

-(void)categoryDining{
    //user's location
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];

CLGeocoder *fgeo = [[CLGeocoder alloc] init];
[fgeo reverseGeocodeLocation:locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
    if (!error) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        zip = placemark.postalCode;

        NSLog(@"%@", zip);
        // Make the URL connection in order to download JSON/XML data.
        NSString *stringSearch = @"http://api.onebigplanet.com/ws/local-daily-deals?wsToken=&wsVersion=3.0.0&numItems=30&out=json&categoryId=11&radiusInMiles=100&zipCode=";
        NSString *combined = [NSString stringWithFormat:@"%@%@", stringSearch, zip];
        NSLog(@"%@",combined);

        NSURL *url = [NSURL URLWithString:combined];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];

        // Error and success message handling.
        [NSURLConnection
         sendAsynchronousRequest:urlRequest
         queue:queue
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
             if([data length] > 0 &&
                error == nil){
                 NSData *jsonData = data;

                 if (jsonData != nil){
                     NSError *error = nil;

                     self.result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

                     if(error == nil)

                         self.mapLocations = result;

                     //NSLog(@"%@", result);

                     for (NSDictionary *location in self.mapLocations[@"merchants"]) {

                         for (NSDictionary *locations in location[@"branches"]) {
                             NSLog(@"%@",locations);
                             CLLocationCoordinate2D annotationCoordinate =
                             CLLocationCoordinate2DMake([locations[@"latitude"] doubleValue],
                                                        [locations[@"longitude"] doubleValue]);
                             Annotation *annotation = [[Annotation alloc] init];
                             annotation.coordinate = annotationCoordinate;
                             annotation.title = locations[@"name"];
                             annotation.subtitle = locations[@"street"];
                             [self.mapView addAnnotation:annotation];

                         }
                     }
                 }
                 else if ([data length] == 0 &&
                          error == nil){
                     NSLog(@"Nothing was downloaded");

                 }
                 else if (error != nil){
                     NSLog(@"Error happened = %@", error);

                 }
             }

         }];

    }
}];

}

我已经通过注销结果对其进行了测试,但它们(标记/图钉)不会显示在地图上。出于某种原因,我认为当我从另一个 viewController 调用方法时,不会为该方法调用 ViewDidLoad 和 ViewWillLoad 方法。我想我需要与某种委派合作,但有点不确定如何去做。任何帮助将不胜感激。

4

1 回答 1

0

看起来您正在使用 alloc init 创建 MapViewController 的新实例,而不是获取对您拥有的实例的引用,即 [array objectAtIndex:[array count]-2]。因此,您的代码应如下所示:

NSArray *array=[self.navigationController viewControllers];
MapViewController *category1 = array[array.count -2];

[category1 categoryDining];
[self.navigationController popToViewController:category1 animated:YES];

如果我正确理解您的设置,我认为这应该可行,但通常当您想将数据发送回另一个控制器时,您会使用委托方法。或者,您可以使用 unwind segue 返回,并以这种方式发送数据。

于 2013-09-16T03:18:28.527 回答