将NSString从一种方法传递到另一种方法

时间:2013-04-03 07:45:51

标签: iphone objective-c nsstring

我遇到了一些新手问题。

我有这个方法来确定iPhone所在的邮政编码,基于GPS坐标。反向地理编码。我的方法有效,我知道这一点,因为将正确的邮政编码写入标签没有问题。

但是我需要将这个邮政编码存储在一个字符串中,我可以在我用来编写短信的方法中使用它。

任何人都可以帮助我吗?

告诉我是否需要源代码! (目前还没有参加我的工作)

显然我被误解了,应该发布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);
        }
    } ];

}

这里我尝试将邮政编码保存在名为postalCode的NSString中(以“**”突出显示)

我尝试在短信作曲家

中再次加载它
-(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];

}

仅在SMS窗口中打印出来: 狗(null) 而且我确定我有坐标,它们会在输出中打印出来。

希望这有助于更好地理解问题

1 个答案:

答案 0 :(得分: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