【问题标题】:Calling User Location from AppDelegate through Different View [closed]通过不同视图从 AppDelegate 调用用户位置 [关闭]
【发布时间】:2012-12-16 00:26:37
【问题描述】:

我正在尝试使用我的 appDelegate 从另一个视图获取用户的位置。我可以将它保存在 NSUserDefaults 中,但我想知道是否有更好的方法。

我的 AppDelegate 包含以下内容:

- (NSString *)deviceLocation
{
NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f", 
locationManager.location.coordinate.latitude, 
locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];

return userCoordinates;

}

然后我想用这个从另一个页面调用用户的位置:

//Get the User's Location
NSString *location =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate deviceLocation];

【问题讨论】:

    标签: objective-c ios xcode cllocationmanager


    【解决方案1】:

    这不起作用,因为 AppDelegate 中的 deviceLocation 方法不返回任何内容。

    如果你想提醒你当前的位置,deviceLocation 应该返回一些东西(即带有当前位置的 NSString)。

    然后用类似这样的方式显示它

    NSString *location =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate deviceLocation];
    
    //Alert
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Location" 
    message:location delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, 
    nil];
    [alert show];
    

    【讨论】:

    • 在代码末尾添加“return latString”时出现两个错误。 1) void 方法设备位置不应返回值。 2) ARC 不允许将目标 c 指针隐式转换为 void。有什么想法吗?
    • 如果您要返回一个对象,则方法签名不应包含 void。 - (NSString *)deviceLocation
    • 另一个奇怪的事情; locationManager 是实例变量吗?为什么在第一次使用后启动它?
    • 我正在处理一些示例代码。这是一种不好的做法吗?
    • 在这种情况下的问题是你为什么要这样做? :) 如果尚未启动,它将失败。如果已经启动,为什么还要再启动一次?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2016-04-12
    • 1970-01-01
    • 2014-01-13
    • 2017-12-26
    相关资源
    最近更新 更多