将NSString从一种方法传递到另一种方法
问题描述:
我已经为自己解决了一些新手问题。将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) 而且我确定我有坐标,它们会在输出中打印出来。
希望这有助于理解这个问题更好
答
#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
刚刚发布你的代码(你如何显示的代码到'UILabel'),并详细阐述您的问题,如果可能的话。 – viral
回答有趣但投票“不是真正的问题”。在你的代码中,你已经在字符串上设置了zip来设置标签,那么问题是什么? – Jano
将字符串存储在ivar中会有帮助吗?然后你可以在你的实例的任何地方访问它。 – HAS