0

好吧,我正在制作一个 iOS 应用程序。我在为 iOS 应用程序编程方面非常陌生...(我确实有编程经验,但这对我来说是全新的)

我正在尝试在屏幕上显示用户的城市和州。我不知道我做错了什么。我有这个:(有一个警告说未使用变量城市和州[我不知道如何解决这个问题])..顺便说一下,这是单一视图,我也在使用故事板(我希望我没有不必..仅使用代码就可以更好地工作)

#import "VOIViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>

@interface VOIViewController ()  <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UILabel *city;
@property (weak, nonatomic) IBOutlet UILabel *state;

@end

@implementation VOIViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)startSignificantChangeUpdates
{
    if ([CLLocationManager locationServicesEnabled])
    {
        if (!self.locationManager)
            self.locationManager = [[CLLocationManager alloc] init];

    self.locationManager.delegate = self;
    [self.locationManager startMonitoringSignificantLocationChanges];
}
}

- (void)stopSignificantChangesUpdates
{
    [self.locationManager stopUpdatingLocation];
    self.locationManager = nil;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

    CLPlacemark *placemark = placemarks[0];
    NSDictionary *addressDictionary = [placemark addressDictionary];
    NSString *city = addressDictionary[(NSString *)kABPersonAddressCityKey];
    NSString *state = addressDictionary[(NSString *)kABPersonAddressStateKey];




}];

[self stopSignificantChangesUpdates];
}

@end

更新:(但屏幕上仍然没有任何内容......我只看到城市和州,但它们实际上并没有显示城市和州。这里的东西没有连接。)

#import "VOIViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>

@interface VOIViewController ()  <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UILabel *city;
@property (weak, nonatomic) IBOutlet UILabel *state;

@end

@implementation VOIViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)startSignificantChangeUpdates
{
    if ([CLLocationManager locationServicesEnabled])
    {
        if (!self.locationManager)
            self.locationManager = [[CLLocationManager alloc] init];

        self.locationManager.delegate = self;
        [self.locationManager startMonitoringSignificantLocationChanges];
    }
}

- (void)stopSignificantChangesUpdates
{
    [self.locationManager stopUpdatingLocation];
    self.locationManager = nil;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        CLPlacemark *placemark = placemarks[0];
        NSDictionary *addressDictionary = [placemark addressDictionary];
        self.city.text = addressDictionary[(NSString *)kABPersonAddressCityKey];
        self.state.text = addressDictionary[(NSString *)kABPersonAddressStateKey];

    }];

    [self stopSignificantChangesUpdates];
}

@end
4

6 回答 6

0

UILabel使用从CLGeocoder响应中获得的地址设置您的 s。

对于该更新- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations如下:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

    CLPlacemark *placemark = placemarks[0];
        NSDictionary *addressDictionary = [placemark addressDictionary];
        NSString *cityText = addressDictionary[(NSString *)kABPersonAddressCityKey];
        NSString *stateText = addressDictionary[(NSString *)kABPersonAddressStateKey];

        self.city.text = [NSString stringWithFormat:@"%@", cityText]; 
        self. state.text = [NSString stringWithFormat:@"%@", stateText];
    }];

    [self stopSignificantChangesUpdates];
}

编辑

你没有 startSignificantChangeUpdates 在任何地方调用方法。那就是问题所在。 在您使用代码中调用它:viewDidLoad [self startSignificantChangeUpdates];

于 2014-03-24T11:29:45.443 回答
0

您没有使用名称与标签名称相同的 NSString 变量 city 和 state。

因此,要更正它,您必须直接将城市和州分配给城市和州标签文本:

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
   CLPlacemark *placemark = placemarks[0];
   NSDictionary *addressDictionary = [placemark addressDictionary];

   /* Delete the code I commented
   NSString *city = addressDictionary[(NSString *)kABPersonAddressCityKey];
   NSString *state = addressDictionary[(NSString *)kABPersonAddressStateKey];
    */

   _city.text = addressDictionary[(NSString *)kABPersonAddressCityKey];
   _state.text = addressDictionary[(NSString *)kABPersonAddressStateKey];
}];

或者您可以将变量 city 和 state 分配给城市和州标签文本:

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
   CLPlacemark *placemark = placemarks[0];
   NSDictionary *addressDictionary = [placemark addressDictionary];
   NSString *city = addressDictionary[(NSString *)kABPersonAddressCityKey];
   NSString *state = addressDictionary[(NSString *)kABPersonAddressStateKey];

   NSLog(@"City => %@", city);
   NSLog(@"State => %@", state);

   _city.text = city;
   _state.text = state;
}];

在你的代码中

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{}

如果你没有连接标签的 IBOutlets,那么首先连接它们,你必须

Ctrl + Click on ViewController in Storyboad and then drag the connecting line to city and state label.

它将在打开的列表中显示名称城市和州,从中选择相应的 IBOutlet 名称。

于 2014-03-24T11:38:32.647 回答
0

您将城市和州名存储在 中NSString,而不是使用它们,因此它会向您显示警告。创建一个标签并将值分配给它们label_name.text = city

于 2014-03-24T11:25:53.287 回答
0

您没有在标签中设置文本。

从你的 locationManager 中移除 NSString *city/state,因为这只会创建一个与你的 UILabel 属性同名的本地 NSString 变量。

然后,改为:

self.city.text = addressDictionary[(NSString *)kABPersonAddressCityKey];
self.state.text = addressDictionary[(NSString *)kABPersonAddressStateKey];

顺便说一句,属性标签中的一个常见约定是包含您正在使用的视图类型。UILabel 通常被命名为 cityLabel,UITextViews 通常被标记为 bodyTextView 等。

此外,请确保您已将属性连接到 Interface Builder(在故事板中)中的实际标签。

于 2014-03-24T11:26:17.330 回答
0

是的,您将 city 和 state 作为NSString's object ,但在任何地方都没有使用它们。

NSString *city = addressDictionary[(NSString *)kABPersonAddressCityKey];
NSString *state = addressDictionary[(NSString *)kABPersonAddressStateKey];

你指定为

city.text= city/// city.text is label .......
state.text = state///// state.text is label.......... 

还将标签对象区分为cityLAbelsateLabel

于 2014-03-24T11:28:16.403 回答
0

你可以这样做

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary = [placemark addressDictionary];

dispatch_async(dispatch_get_main_queue(), ^{
        self.city.text = addressDictionary[(NSString *)kABPersonAddressCityKey];
        self.state.text = addressDictionary[(NSString *)kABPersonAddressStateKey];
    });


}];
于 2014-03-24T11:28:25.487 回答