2013-04-10 39 views
0

我實際上使用核心位置以及地圖工具包框架來獲取按鈕點擊時的用戶位置。我有兩個視圖控制器。第一個是一個普通的視圖控制器,其中包含地圖視圖以及在其中完成的所有核心位置處理以及反向地理編碼。 第二個是表格視圖控制器,顯示從地理編碼和位置的經緯度獲得的數據。將數據傳遞到目標視圖控制器

我得到的問題是我無法將當前位置的信息傳遞給下一個視圖。但是,我可以傳遞我的地理編碼值。這對我來說似乎很奇怪,任何幫助都會令我非常感激。

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 
    }]; 

} 
+1

您已經嘗試了什麼?有這麼多問題。去谷歌上查詢! – brianLikeApple 2013-04-10 13:37:38

+0

'Xcode'標籤只應用於有關Xcode工具本身的問題,而不是針對使用Xcode的編程問題。 – 2013-04-10 13:52:59

回答

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"); 

} 
} 
+0

非常感謝阿尼爾:)它的工作..一個簡單和完美的解決方案..謝謝.. – Raj0689 2013-04-10 14:11:45

0
  1. 你不應該在電流控制器保存addressViewController因爲它挽留他,直到你再次進行SEGUE。而且在某些執行階段,它至少是無用的,甚至是有害的。
  2. view.currentLocation = newOne; // assigning currentLocation the same location as the newOne 此時的視圖可以包含nill,因此任何指定給它的屬性都不會更正。您應該將currentLocation保存在當前視圖控制器的屬性中,然後在prepareForSegue中將其設置爲destinationViewController
  3. 我不想談論你的代碼命名約定,但是當我看到從小寫字母開頭的類名=)
+0

我瞭解@Ossir命名約定。正如你所提到的那樣,我將徹底改變它們。我跟他們一樣,我不應該做的屬性。感謝您指出了這一點。爲什麼我沒有早點意識到它。而且,我是編程新手,因此這些錯誤:( – Raj0689 2013-04-10 14:14:06

相關問題