将数据传递到目标视图控制器

时间:2013-04-10 13:29:08

标签: ios xcode mapkit core-location

我实际上正在使用核心位置以及地图工具包框架来获取按钮单击时的用户位置。我有两个视图控制器。第一个是普通视图控制器,其中包含地图视图和在其中完成的所有核心位置过程以及反向地理编码。 第二个是表视图控制器,它显示从地理编码和位置的纬度和经度获得的数据。

我得到的问题是我无法将当前位置的信息传递给下一个视图。但是,我可以传递我的地理编码值。这对我来说似乎很奇怪,任何帮助都会非常感激。

mapViewController.m

#import "mapViewController.h"

@interface mapViewController ()
{
    CLLocationManager *locationManager;
    CLGeocoder *geoCoder;
    CLPlacemark *placemark;
    addressViewController *view;
}

@end

@implementation mapViewController
@synthesize mapView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    locationManager = [[CLLocationManager alloc]init];
    geoCoder = [[CLGeocoder alloc]init];
    self.addressOutlet.enabled = NO;
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"addressSegue"])
    {
        view= segue.destinationViewController;

         // assigning the addressPlacemark the same value as the current placemark
        view.addressPlacemark = placemark;

        NSLog(@"this is perfectly executed");

    }
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 600, 600);
    [self.mapView setRegion:region animated:YES];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newOne = [locations lastObject];

    [locationManager stopUpdatingLocation];
    NSLog(@"the new location is : %@", newOne);
    NSLog(@"the latitude is %f", newOne.coordinate.latitude);
    NSLog(@"the speed is %.2f mps", newOne.speed);
    NSLog(@"at time : %@",newOne.timestamp);

//These 4 NSLog statements are returning the outputs as expected


 [geoCoder reverseGeocodeLocation:newOne completionHandler:^(NSArray *placemarks, NSError *error) {
        placemark = [placemarks objectAtIndex:0];
        NSLog(@"the current city is %@",placemark.locality);

        self.addressOutlet.enabled = YES;
        // view.currentLocation = [[CLLocation alloc]init];

        view.currentLocation = newOne;  // assigning currentLocation the same location as the newOne

        NSLog(@"the currentLocation object has the location : \n %@",view.currentLocation);
        //problem : the output of this NSLog gives Null
    }];

}

2 个答案:

答案 0 :(得分:1)

问题在于

view.currentLocation = newOne;

在此背景下,视图为零。调用 prepareForSegue:时实例化视图。所以你能做的就是

tempLocation = newOne; // Store newOne in a temporary variable

并在

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"addressSegue"])
{
    view= segue.destinationViewController;

     // assigning the addressPlacemark the same value as the current placemark
    view.addressPlacemark = placemark;
     view.currentLocation = tempLocation;
    NSLog(@"this is perfectly executed");

}
}

答案 1 :(得分:0)

  1. 您不应该将addressViewController保存在当前控制器中,因为它会保留他直到您再次执行segue。在某些执行点上,它至少是无用的,甚至是有害的。
  2. view.currentLocation = newOne; // assigning currentLocation the same location as the newOne 此时的视图可以包含nill,因此任何分配给它的属性都不正确。您应该将currentLocation保存在当前视图控制器的属性中,然后在prepareForSegue中将其设置为destinationViewController
  3. 我不想谈论你的代码命名约定,但当我看到以小写字母开头的类名时,它让我很困惑=)