Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS xCode/Objective C-简单-显示用户';s城市作为标签?_Ios_Location_Label_City - Fatal编程技术网

iOS xCode/Objective C-简单-显示用户';s城市作为标签?

iOS xCode/Objective C-简单-显示用户';s城市作为标签?,ios,location,label,city,Ios,Location,Label,City,好的,我正在制作一个iOS应用程序。。我对iOS应用程序编程非常陌生。。。(我确实有编程经验,但这对我来说是全新的) 我试图在屏幕上显示用户的城市和州。。我不知道我做错了什么。我有这样一个警告:(有一个警告说未使用的变量城市和州[我不知道如何修复])。。顺便说一下,这是单一视图,我也在使用故事板(我希望我不必..仅仅使用代码就可以更好地工作) #导入“VOIViewController.h” #进口 #进口 @接口VOIViewController() @属性(非原子,强)CLLocationM

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

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

#导入“VOIViewController.h”
#进口
#进口
@接口VOIViewController()
@属性(非原子,强)CLLocationManager*locationManager;
@属性(弱、非原子)IBUILabel*城市;
@属性(弱,非原子)IBUILabel*状态;
@结束
@实现虚拟控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)开始重要变更更新
{
如果([CLLocationManager LocationServiceEnabled])
{
如果(!self.locationManager)
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
[self.locationManager启动监视重要位置更改];
}
}
-(作废)停止重大变更日期
{
[self.locationManager停止更新位置];
self.locationManager=nil;
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置
{
CLLocation*location=[locations lastObject];
CLGeocoder*geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:位置完成处理程序:^(NSArray*位置标记,NSError*错误){
CLPlacemark*placemark=placemarks[0];
NSDictionary*addressDictionary=[placemark addressDictionary];
NSString*city=addressDictionary[(NSString*)KabPersonalAddressCityKey];
NSString*state=addressDictionary[(NSString*)KabPersonalAddressStateKey];
}];
[自停止重大变更日期];
}
@结束
更新:(但屏幕上仍然没有显示任何内容…我只看到城市和州,但它们实际上没有显示城市和州。这里有些东西没有连接。)

#导入“VOIViewController.h”
#进口
#进口
@接口VOIViewController()
@属性(非原子,强)CLLocationManager*locationManager;
@属性(弱、非原子)IBUILabel*城市;
@属性(弱,非原子)IBUILabel*状态;
@结束
@实现虚拟控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)开始重要变更更新
{
如果([CLLocationManager LocationServiceEnabled])
{
如果(!self.locationManager)
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
[self.locationManager启动监视重要位置更改];
}
}
-(作废)停止重大变更日期
{
[self.locationManager停止更新位置];
self.locationManager=nil;
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置
{
CLLocation*location=[locations lastObject];
CLGeocoder*geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:位置完成处理程序:^(NSArray*位置标记,NSError*错误){
CLPlacemark*placemark=placemarks[0];
NSDictionary*addressDictionary=[placemark addressDictionary];
self.city.text=addressDictionary[(NSString*)KabPersonalAddressCityKey];
self.state.text=addressDictionary[(NSString*)KabPersonalAddressStateKey];
}];
[自停止重大变更日期];
}
@结束

您正在将城市和州名称存储在
NSString
中,未使用它们,因此显示警告。
创建一个标签并为其赋值为
label_name.text=city

您没有在标签中设置文本

从locationManager中删除NSString*城市/州,因为这只会创建与UILabel属性同名的本地NSString变量

然后,取而代之的是:

self.city.text = addressDictionary[(NSString *)kABPersonAddressCityKey];
self.state.text = addressDictionary[(NSString *)kABPersonAddressStateKey];
顺便说一下,特性标签中的一个常见约定是包含您正在使用的视图类型。UILabel通常被命名为cityLabel,UITextView通常被命名为bodyTextView等


另外,请确保已将属性连接到Interface Builder(在情节提要中)中的实际标签。

是的,您将城市和州作为
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

,您可以在

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


}];

使用从
CLGeocoder
响应获取的地址设置
UILabel
s

对于该更新
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)位置
,如下所示:

- (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];
}
编辑


您还没有在任何地方调用
启动重大更改更新
方法。这就是问题所在。在您的
视图中调用它,使用以下代码加载
[self startSignificantChangeUpdates]

您没有使用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];
}];
[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;
}];
也可以将变量city和state指定给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];
}];
[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{}
如果您没有连接标签的IB插座,则必须先连接它们

Ctrl + Click on ViewController in Storyboad and then drag the connecting line to city and state label.
它将向您显示名称c