在方法之间传递NSString

时间:2013-04-05 09:44:15

标签: iphone objective-c methods 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) 而且我确定我有坐标,它们会在输出中打印出来。

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

3 个答案:

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

答案 1 :(得分:0)

尝试

picker.body = [NSString stringWithFormat:@"Dog %@", postalCode];

答案 2 :(得分: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];
}