2013-04-03 157 views
-1

我已經爲自己解決了一些新手問題。將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) 而且我確定我有座標,它們會在輸出中打印出來。

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

+1

剛剛發佈你的代碼(你如何顯示的代碼到'UILabel'),並詳細闡述您的問題,如果可能的話。 – viral

+0

回答有趣但投票「不是真正的問題」。在你的代碼中,你已經在字符串上設置了zip來設置標籤,那麼問題是什麼? – Jano

+0

將字符串存儲在ivar中會有幫助嗎?然後你可以在你的實例的任何地方訪問它。 – HAS

回答

1
#import <CoreLocation/CoreLocation.h> 

@interface SomeController : UIViewController <CLLocationManagerDelegate> 
@property (strong,nonatomic) CLLocationManager *locationManager; 
@end 


@implementation SomeController 

-(void) trackUpdates 
{  
    self.locationManager = [CLLocationManager new]; 
    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
    self.locationManager.distanceFilter = 10.0f; 
    if ([CLLocationManager locationServicesEnabled]){ 
     [self.locationManager startUpdatingLocation]; 
    } 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(NSArray *)locations 
{ 
    CLGeocoder* gcrev = [[CLGeocoder alloc] init]; 
    [gcrev reverseGeocodeLocation:[locations lastObject] 
       completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     CLPlacemark* revMark = [placemarks objectAtIndex:0]; 
     NSString *zip = [revMark.addressDictionary objectForKey:@"ZIP"]; 
     NSLog(@"%@",zip); 
     // ... do something with the zip, store in an ivar, call a method, etc. 
    }]; 
} 

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    [self trackUpdates]; 
} 

@end 
相關問題