2013-04-05 80 views
0

我已經有了一些新手問題。在方法之間傳遞NSString

我得到了這個方法來根據GPS座標確定iPhone所在的郵政編碼。反向地理編碼。我的方法很有效,我知道這一點,因爲將正確的郵政編碼寫入標籤沒有問題。

但我需要這個郵政編碼被存儲在一個字符串,我可以使用一個方法,我用來組成一個短信。

任何人都可以幫助我嗎?

告訴我,如果源代碼是必要的! (只是目前沒有坐在我的工作comp)

很明顯,我一直誤解,應該發佈som代碼。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 


    // Reverse Geocoding 
    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
      **postalCode** = [NSString stringWithFormat:@"%@", 
          placemark.postalCode]; 
     } else { 
      NSLog(@"%@", error.debugDescription); 
     } 
    } ]; 

} 

在這裏,我嘗試保存的NSString命名POSTALCODE裏面的郵政編碼(帶強調「**」)

我嘗試在短信作曲家再次裝入

-(void)displaySMSComposerSheet 
{ 
    CLLocation *currentLocation; 


    // Reverse Geocoding 
    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
      postalCode = [NSString stringWithFormat:@"%@", 
          placemark.postalCode]; 
     } else { 
      NSLog(@"%@", error.debugDescription); 
     } 
    } ]; 

    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; 
    picker.recipients = [NSArray arrayWithObjects:@"1220", nil]; 
    picker.body = [NSString stringWithFormat:@"Dog %@", (placemark.)postalCode]; 
    picker.messageComposeDelegate = self; 

    [self presentModalViewController:picker animated:YES]; 

} 

只打印在短信窗口中輸出: Dog(null) 而且我確定我有座標,它們會在輸出中打印出來。

希望這有助於理解這個問題更好

+0

u需要保留'postalCode'當u從谷歌的API獲取郵政編碼後,分配給它.. – vishy 2013-04-05 09:52:52

+0

都在同一類? – 2013-04-05 09:54:14

回答

1

如果您要訪問整個viewControllers postalCode,你可以只將其保存到NSUserDefaults

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // ... 
    NSString *postalCode = [NSString stringWithFormat:@"%@", 
          placemark.postalCode]; 
    [[NSUserDefaults standardDefaults] setObject:postalCode forKey:@"postalCode"]; 
    // ... 
} 

,並再次將其裝入

-(void)displaySMSComposerSheet 
{ 
    // ... 
    NSString *postalCode = [[NSUserDefaults standardDefaults] objectForKey:@"postalCode"]; 
    // .... 
} 

編輯:你確定placemarkpostalCode是已經設定(即 - locationManager:didUpdateToLocation:fromLocation:已被調用)?如果在確定位置之前調用displaySMSComposerSheet,則此行爲是正常的。

編輯2:從您的代碼,它看起來像你忘了告訴locationManager開始更新位置。以下內容添加到viewDidLoad

locationManager.delegate = self; 
[locationManager startUpdatingLocation]; 
+0

似乎我得到了同樣的答案 (地標。)postalCode「(」「)」只是爲了表明我已經嘗試了兩件事情。只有郵政編碼仍然返回(空)在SMS內 – 4FunAndProfit 2013-04-05 11:59:51

+0

剛更新我的回答 – tilo 2013-04-05 12:10:24

+0

你的意思就像我在這個例子中所做的? 剛將全班複製到pastebin中 postNr = postalCode(這只是丹麥語單詞) http:// pastebin。COM/ZVQ1dNWH – 4FunAndProfit 2013-04-05 12:58:43

0

嘗試

picker.body = [NSString stringWithFormat:@"Dog %@", postalCode]; 
0
-(void)displaySMSComposerSheet 
{ 
    CLLocation *currentLocation; 


    // Reverse Geocoding 
    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
      postalCode = [NSString stringWithFormat:@"%@", 
          placemark.postalCode]; 
      [self showMailComposer:postalcode]; // method call 
     } else { 
      NSLog(@"%@", error.debugDescription); 
     } 
    } ]; 



} 

-(void)showMailComposer:(NSString *)postalCode{ 
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init]; 
    picker.recipients = [NSArray arrayWithObjects:@"1220", nil]; 
    picker.body = [NSString stringWithFormat:@"Dog %@", postalCode]; 
    picker.messageComposeDelegate = self; 

    [self presentModalViewController:picker animated:YES]; 
} 
+0

感謝您的回答。 我試過這個,它只是不會打開顯示短信。 將完整的ViewController.m文件發佈到pastebin上。 http://pastebin.com/ZVQ1dNWH – 4FunAndProfit 2013-04-05 13:03:00