【发布时间】: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