首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ios中实现CLLocationManager的CLLocationManager委托方法

在ios中实现CLLocationManager的CLLocationManager委托方法
EN

Stack Overflow用户
提问于 2014-12-18 11:03:58
回答 1查看 4K关注 0票数 3

我已经在-(void)localnotification中实现了DashboardController.m.Now,我希望访问localnotification并将didUpdateLocations委托方法实现到setingsController.m类中。到目前为止,我的方法是:

DashBoardViewController.h

代码语言:javascript
复制
#import <CoreLocation/CoreLocation.h>

@class AppDelegate;
@interface DashBoardViewController : UIViewController<CLLocationManagerDelegate, UIAlertViewDelegate>{

    AppDelegate *appDel;
}
@property(nonatomic,strong) CLLocationManager *locationManager;
@property (nonatomic,retain) CLLocation *current;

-(void)localnotification;
@end

DashBoardViewController.m

代码语言:javascript
复制
#import "DashBoardViewController.h"
#import "AppDelegate.h"

@interface DashBoardViewController ()
@end 
@implementation DashBoardViewController

@synthesize current,locationManager;
- (void)viewDidLoad
{
    [super viewDidLoad];
    appDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [self localnotification];
}

-(void)localnotification{ 
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startMonitoringSignificantLocationChanges];
}

在SettingController.m中,我访问的内容如下:

代码语言:javascript
复制
@implementation SettingsViewController
DashBoardViewController *dash;

- (void)viewDidLoad {
    [super viewDidLoad];
    appDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    dash=[[DashBoardViewController alloc] init];
    [dash localnotification];
    NSArray *locations;
    [self locationManager:appDel.locationManager didUpdateLocations:locations];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //location 1
    CLLocation *locA = [[CLLocation alloc] initWithLatitude:appDel.latVal longitude:appDel.longVal];

    //location 2
    CLLocation *locB = [[CLLocation alloc] initWithLatitude:appDel.locationManager.location.coordinate.latitude longitude:appDel.locationManager.location.coordinate.longitude ];

    appDel.latVal = appDel.locationManager.location.coordinate.latitude; //Latitute of the location
    appDel.longVal = appDel.locationManager.location.coordinate.longitude; //Longitude of the location

    //Difference between location 1 & location 2
    CLLocationDistance dist = [locA distanceFromLocation:locB];
    NSLog(@"Difference from Settings: %ld",lroundf(dist));
    NSLog(@"Last Location's Object Settings: %@",[locations lastObject]);
}

但是lastObject在settingsController类中返回null。在视图控制器类中,我不需要定义位置数组,但是在设置类中,因为我只访问了方法,所以我必须声明位置数组,否则就会产生错误。我该通过什么?

代码语言:javascript
复制
[self locationManager:appDel.locationManager didUpdateLocations:?];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-18 11:36:53

以下是在多视图控制器中使用位置管理器的最佳方法:

AppDelegate.h

代码语言:javascript
复制
@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) CLLocationManager * locationManager;
@property (nonatomic, strong) CLLocation *currentLocation;

AppDelegate.m

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    if([CLLocationManager locationServicesEnabled]){
        currentLocation_ = [[CLLocation alloc] init];
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        [locationManager startUpdatingLocation];
    }
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    currentLocation_ = newLocation;
}

现在在DashBoardViewController & SettingsViewController

@property (强,非原子) CLLocation *currentLocation;

代码语言:javascript
复制
- (void)viewDidLoad{
    [super viewDidLoad];
    if([CLLocationManager locationServicesEnabled]){
        AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
        currentLocation_ = [[CLLocation alloc] initWithLatitude:appDelegate.currentLocation.coordinate.latitude longitude:appDelegate.currentLocation.coordinate.longitude];
    }
}

注意:举个例子, Its,您可以根据需要对其进行改进。

希望它能对你有用。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27545049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档