【问题标题】:iOS xCode/Objective C - Simple - Display user's city as a label?iOS xCode/Objective C - 简单 - 将用户的城市显示为标签?
【发布时间】:2014-05-01 17:23:54
【问题描述】:

好吧,我正在制作一个 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

【问题讨论】:

  • 尝试粘贴更新后的代码
  • 是的,我做到了,但我仍然没有在屏幕上收到任何内容。
  • 我已经编辑了我的答案。请检查一下。

标签: ios location label city


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    是的,您将 city 和 state 作为 NSString 的对象,但不会在任何地方使用它们。

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

    你指定为

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

    还将标签对象区分为cityLAbel 和sateLabel

    【讨论】:

    • 抱歉,我应该把 city.text = city 和 state.text = state 放在哪里?
    • 你的方法locationManager
    【解决方案3】:

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

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

    然后,改为:

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

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

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

    【讨论】:

      【解决方案4】:

      你可以这样做

      [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];
          });
      
      
      }];
      

      【讨论】:

      • 我在一定程度上理解您的操作 - 但屏幕上没有显示任何内容。
      • 这不起作用,因为您不能直接在城市或州的字符串中设置。您需要在标签的 .text 属性上进行设置。
      • @Handsomeguy 这是一个错误,现在我已经编辑了它。
      • 我明白了。它缺少.text。现在没有警告出现,但模拟器上仍然没有显示?我在故事板上制作了一个城市和一个州的标签。
      • 事实上,澄清这是唯一的表现。我在故事板上放了一个“城市”和“州”,这是唯一显示的词 - 但不是城市和州。
      【解决方案5】:

      将您的UILabels 设置为从CLGeocoder 响应中获得的地址。

      对于该更新- (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];

      【讨论】:

      • 兄弟,谢谢!现在只剩下一个小问题了。城市更改为 CA,而州则不做任何事情。它只是保持“状态”.. 为什么城市是 CA?
      【解决方案6】:

      您没有使用名称与标签名称相同的 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 名称。

      【讨论】:

      • 是的,我确实控制了 + 单击 ViewController,但屏幕上除了文本“城市”和“州”之外仍然没有任何内容
      • 在 _city.text 赋值中添加断点,然后运行项目进行调试。它会在一段时间后进入,然后逐行检查。
      • 您可以使用 NSLog() 函数记录城市和州以检查反向地理编码。在第二个区块检查我的代码版本。
      猜你喜欢
      • 2016-01-07
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      相关资源
      最近更新 更多