【问题标题】:implement didUpdateLocations delegate method of CLLocationManager in ios在ios中实现CLLocationManager的didUpdateLocations委托方法
【发布时间】:2014-12-18 11:03:58
【问题描述】:

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

DashBoardViewController.h

#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

#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 我这样访问:

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

但是 [locations lastObject] 在 settingsController 类中返回 null。在视图控制器类中,我不必定义 locations 数组,但在设置类中,因为我只访问 localnotification 方法,我已经声明了位置数组,否则会出错。我应该通过什么

[self locationManager:appDel.locationManager didUpdateLocations:?];

【问题讨论】:

  • 您是否仅在 iOS 8 中遇到此问题?
  • 我的开发目标是7.1。我不明白,在我实现这两种方法的 DashBoardViewController 中,它工作得很好。但是在 SettingController 中,如果我没有声明 locations 数组,我调用了 localnotification 和 didUpdateLocations 方法,它给出了错误。
  • 如果它已经在 1 页上工作并且除了你可能缺少一些东西之外没有工作。尝试在它不起作用的页面中再次实现
  • 你能再看看settingsController类的代码吗?这是调用委托方法的正确方法还是我必须调用委托方法?我的意思是在 dashboardviewconroller 类中,我什至不必在 viewDidLoad 中调用 didUpdateLocations 方法,但在设置 conntoller 如果我不调用 didUpdateLocations 方法,那么它不会自动调用。
  • 请检查我的答案

标签: ios objective-c cllocationmanager


【解决方案1】:

这是在多个视图控制器中使用位置管理器的最佳方式:

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) CLLocationManager * locationManager;
@property (nonatomic, strong) CLLocation *currentLocation;

AppDelegate.m

- (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 (strong, nonatomic) CLLocation *currentLocation;

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

注意: 以它为例,您可以根据需要对其进行改进。

希望它对你有用。

【讨论】:

  • 好的,我会实施这个!
  • 如何在不使用dashboardcontroller中的NSTimer的情况下将位置地址字符串从appdelegate设置到dashboardcontroller UILabel?
  • @Poles 作为cyberlobe 的答案,您应该始终使用单例模式来处理这类事情。使用AppDelegate,因为它实现起来更快。如果我正在构建一个严肃的应用程序,我会选择单例模式。
猜你喜欢
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 2016-05-12
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多