下午好一切,已經在自定義類對象在我的ViewController可以看出,試圖與CLGeocoder創建的LocationManager
我試圖完成的LocationManager和CLGeocoder的教程,但我無法理解如何將這些與一個互動另一類通過。
爲了解釋,我按照教程顯示了當前的位置。現在我試圖讓CLGeocoder在該位置返回信息,但由於locationManager位於不同的類中,所以我遇到了麻煩。我的問題是瞭解自定義類以及視圖控制器如何將它們連接在一起。
這是我的代碼到目前爲止。
appdelegate.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中找到了位置。這是我的問題,我不明白我在視圖控制器中的位置。我來自一個C#背景,我只是傳遞一個變量,或者創建一個全局變量。
有人可以請解釋IOS中通信對象的最佳方式。 如果我需要更好地解釋自己,請讓我知道。
THANKs !!!
你是對的,因爲實際上並不需要將cllocation管理器放在一個單獨的類中,但是我想盡可能學習ios,我覺得這是一個很好的學習練習。因此,請你告訴我如何讓locationgetter成爲應用程序代理的一個屬性。我看到你會使用示例代碼,但我在哪裏放置了這兩行代碼。感謝您的小點。我也會修復這些問題 – solarissf 2013-03-03 23:51:52
重新讀你的代碼我發現你實際上並不需要公開'LocationGetter' - 我假設AppDelegate有一個'LocationGetterDelegate'協議方法的實現,它設置委託的'lastKnownLocation'屬性到傳入的位置。我已經相應地修改了我的答案。我仍然認爲這樣做有點複雜,但嘿。 – 2013-03-04 00:40:42
謝謝....我採取了你的建議,我決定保持簡單,並讓所有工作在viewcontroller中。一旦我有了更好的objective-c基礎,我將開始使用自定義類。欣賞幫助! – solarissf 2013-03-06 01:43:15