Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.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 将NSObject中didUpdateLocations中的NSString传递给UIViewController_Ios_Objective C_Iphone - Fatal编程技术网

Ios 将NSObject中didUpdateLocations中的NSString传递给UIViewController

Ios 将NSObject中didUpdateLocations中的NSString传递给UIViewController,ios,objective-c,iphone,Ios,Objective C,Iphone,我创建了一个iOS项目,并实现了didUpdateLocations,以获取我在NSObject中的位置。我想获得从代理到UIViewController的经度和纬度,但结果总是空的,即使是测试addObserver,我也做不到。在底部有一个我做的样本,它显示了我在标签上的位置。我在viewcontroller中实现了didUpdateLocations,没有NSObject类 这是NSObject中的我的代码: #import "GpsModel.h" @implementation Gps

我创建了一个iOS项目,并实现了
didUpdateLocations
,以获取我在
NSObject
中的位置。我想获得从代理到
UIViewController
的经度和纬度,但结果总是空的,即使是测试
addObserver
,我也做不到。在底部有一个我做的样本,它显示了我在标签上的位置。我在viewcontroller中实现了
didUpdateLocations
,没有
NSObject

这是
NSObject
中的我的代码:

#import "GpsModel.h"

@implementation GpsModel{
    CLLocation *crnLoc;
}

@synthesize locationManager;
@synthesize longitude;
@synthesize latitude;

- (void) initLocation {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];
    [locationManager startUpdatingLocation];

}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    //[self showAlert];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    crnLoc = [locations lastObject];
    longitude = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
    latitude = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
}

@end
这是我的ViewController:

#import "GpsViewController.h"

@interface GpsViewController ()

@end

@implementation GpsViewController{
    CLLocationManager *locationManager;
}

@synthesize gpsView;
@synthesize gpsModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    gpsView = [[GpsView alloc] initWithFrame:CGRectMake(0, 0, widthtScreen, heightScreen)];
    [self.view addSubview:gpsView];
    gpsModel = [GpsModel new];
    [gpsModel initLocation];
    gpsView.longitudeLabel.text = gpsModel.longitude;
    [gpsView.longitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    gpsView.latitudeLabel.text = gpsModel.latitude;
    [gpsView.latitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if(object == gpsView.longitudeLabel)
    {
        NSLog(@"long");
    }
    if(object == gpsView.latitudeLabel)
    {
        NSLog(@"lat");
    }
}
@end
这是我的代码,无需创建NSObject类:

@interface GpsViewController ()

@end

@implementation GpsViewController{
    CLLocationManager *locationManager;
    CLLocation *crnLoc;
}

@synthesize gpsView;
@synthesize gpsModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    gpsView = [[GpsView alloc] initWithFrame:CGRectMake(0, 0, widthtScreen, heightScreen)];
    [self.view addSubview:gpsView];
    [self initLocation];

}
- (void) initLocation {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];
    [locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    [self showAlert];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    crnLoc = [locations lastObject];
    NSLog(@"position long : %@",[NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude]);
    NSLog(@"position lat : %@",[NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude]);
    gpsView.longitudeLabel.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
    [gpsView.longitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    gpsView.latitudeLabel.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude];
    [gpsView.latitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if(object == gpsView.longitudeLabel)
    {
        NSLog(@"observe1");

    }
    if(object == gpsView.latitudeLabel)
    {
        NSLog(@"observe2");

    }
}
这是我的视图,包含两个标签

@implementation GpsView{
    CGFloat _originY;
}

@synthesize longitudeLabel;
@synthesize latitudeLabel;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _originY = 50;
        [self _initView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self _initView];
    }
    return self;
}

- (void)_initComponents {
    longitudeLabel = [[UILabel alloc] init];
    latitudeLabel = [[UILabel alloc] init];
}

- (void)_initView {
    [self _initComponents];
    [self _createComponentView];
}

-(void) _createComponentView {
    _originY = [self createLabel:_originY :longitudeLabel :self];
    [self addSubview:longitudeLabel];
    _originY = [self createLabel:_originY :latitudeLabel :self];
    [self addSubview:latitudeLabel];
}

您正在观察标签中的文本,该标签在didLoad方法中只设置了一次,并且在位置更新后将永远不会更新。如果您想使用观察者,您应该观察其中的gpsModel实例和纬度/经度属性。在observeValueForKeyPath委托方法中,您应该更新文本标签。

您使用的是类似的还是真实的设备?是否
crnLoc
等于
nil
?是的,我在实际设备中使用过它。实际上,crnLoc不是零。我在控制台中显示了我的经度和纬度,但仅在didUpdateLocations中显示。在观察标签中的文本之前,我无法在标签中显示经度。我从我的模型中得到空值。我在哪里可以更新视图控制器中的标签?只有当CoreLocation接收到第一次位置更新时,您才能显示正确的值。在此之前,位置将为空,这是正确的。正如我所说的,如果你想使用观察者,你应该观察你的gpsModel和observeValueForKeyPath中的值,你将能够更新你的标签。对于观察者,我知道了,但对于标签,这是最难的部分。我尝试将所有内容都放在同一个viewcontroller中,它会在标签中显示文本。它工作得很好。但是当我改为nsobject时,我无法从中获取字符串