大家下午好,
我正在尝试完成有关 locationManager 和 CLGeocoder 的教程,但无法理解它们如何通过类相互交互。
为了解释,我按照教程显示了当前位置的工作。现在我试图让 CLGeocoder 返回关于该位置的信息,但我遇到了麻烦,因为 locationManager 在不同的类中。我的问题是理解自定义类以及视图控制器如何将它们联系在一起。
到目前为止,这是我的代码。
应用代理.h
#import <UIKit/UIKit.h>
#import "LocationGetter.h"
#import <UIKit/UIKit.h>
#import "LocationGetter.h"
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) UIWindow *window;
@property (retain, nonatomic) ViewController *viewController;
@property (nonatomic, strong) CLLocation *lastKnownLocation;
@end
appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
self.locationGetter = [[LocationGetter alloc] init];
self.locationGetter.delegate = self;
[self.locationGetter startUpdates];
类 locationgetter.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@protocol LocationGetterDelegate <NSObject>
@required
- (void) newPhysicalLocation:(CLLocation *)location;
@end
@interface LocationGetter : NSObject
-(void)startUpdates;
@property (nonatomic, weak) id<LocationGetterDelegate>delegate;
@end
locationgetter.m
#import "LocationGetter.h"
#import <CoreLocation/CoreLocation.h>
@interface LocationGetter () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation LocationGetter
//@synthesize geoCoder;
bool didUpdate = NO;
-(void) startUpdates{
NSLog(@"Starting Location Updates");
if (self.locationManager == nil)
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your location could not be determined." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (didUpdate)
return;
didUpdate = YES;
// Disable future updates to save power.
[manager stopUpdatingLocation];
// let our delegate know we're done
[_delegate newPhysicalLocation:newLocation];
}
@end
现在问题来了……
我需要在某个地方放入 CLGeocoder 属性,以及启动 CLGeocoder 的按钮的操作。我试着把它放在视图控制器中
@property (strong, nonatomic) IBOutlet CLGeocoder *geoCoder;
- (IBAction)geoCodeLocation:(id)sender;
这在 viewcontroller.m
- (IBAction)geoCodeLocation:(id)sender {
NSLog(@"hello %@,", sender);
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
}
但是按照我的方式,找不到locationManager。我猜是因为在 AppDelegate.M 中找到了位置。这是我的问题,我不明白在视图控制器中如何才能看到位置。我来自 ac# 背景,我只会传递一个变量,或者创建一个全局变量。
有人可以解释在 IOS 中通信对象的最佳方式吗?如果我需要更好地解释自己,请告诉我。
谢谢!!!