将数据传递到目标视图控制器
我实际上使用核心位置以及地图工具包框架来获取按钮点击时的用户位置。我有两个视图控制器。第一个是一个普通的视图控制器,其中包含地图视图以及在其中完成的所有核心位置处理以及反向地理编码。 第二个是表格视图控制器,显示从地理编码和位置的经纬度获得的数据。将数据传递到目标视图控制器
我得到的问题是我无法将当前位置的信息传递给下一个视图。但是,我可以传递我的地理编码值。这对我来说似乎很奇怪,任何帮助都会令我非常感激。
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
}];
}
问题在于
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");
}
}
非常感谢阿尼尔:)它的工作..一个简单和完美的解决方案..谢谢.. – Raj0689 2013-04-10 14:11:45
- 你不应该在电流控制器保存
addressViewController
因为它挽留他,直到你再次进行SEGUE。而且在某些执行阶段,它至少是无用的,甚至是有害的。 -
view.currentLocation = newOne; // assigning currentLocation the same location as the newOne
此时的视图可以包含nill,因此任何指定给它的属性都不会更正。您应该将currentLocation
保存在当前视图控制器的属性中,然后在prepareForSegue
中将其设置为destinationViewController
。 - 我不想谈论你的代码命名约定,但是当我看到从小写字母开头的类名=)
我了解@Ossir命名约定。正如你所提到的那样,我将彻底改变它们。我跟他们一样,我不应该做的属性。感谢您指出了这一点。为什么我没有早点意识到它。而且,我是编程新手,因此这些错误:( – Raj0689 2013-04-10 14:14:06
您已经尝试了什么?有这么多问题。去谷歌上查询! – brianLikeApple 2013-04-10 13:37:38
'Xcode'标签只应用于有关Xcode工具本身的问题,而不是针对使用Xcode的编程问题。 – 2013-04-10 13:52:59