我想爲我的MKMapView打開用戶跟蹤模式。 MKMapView是故事板的一部分,所以我相信它會自動初始化。當我在viewDidLoad
和viewDidAppear
上運行調試器時,它會打印MKMapView的地址,因此它似乎已被初始化。下面是我在我的FirstViewController類方法:在類中的某些方法不能識別MKMapView的實例已經存在
#import "FirstViewController.h"
#import "Options.h"
#import "AppDelegate.h"
@interface FirstViewController()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.currentMap.delegate = self;
[appDelegate.locationManager requestAlwaysAuthorization];
appDelegate.locationManager.distanceFilter = 20.0;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:NO];
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways)
{
UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Cannot Access Location"
message:@"Please allow Location Services in Settings."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert show];
}
self.currentMap.showsUserLocation = [[NSUserDefaults standardUserDefaults] boolForKey:@"mapLocation"];
}
- (void)changeTracking {
if (self.currentMap.userTrackingMode == MKUserTrackingModeNone)
{
[self.currentMap setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
else if (self.currentMap.userTrackingMode == MKUserTrackingModeFollow)
{
[self.currentMap setUserTrackingMode:MKUserTrackingModeNone animated:YES];
}
}
當我打電話[appdelegate.firstViewController changeTracking]
從另一個類的changeTracking
方法執行,但它認爲self.currentMap
是nil
。因此,當它更改用戶跟蹤模式時,它實際上對viewDidLoad
和viewDidAppear
識別的地圖沒有任何影響。爲什麼前兩個方法可以識別MKMapView的實例,但是changeTracking
沒有?
您是否可能將地圖視圖用戶跟蹤模式設置爲'.FollowWithHeading'?因爲如果這是你的初始狀態,你的'-changeTracking'只能處理其他兩種模式,並且永遠不會執行'if'模塊。 – incanus 2014-12-10 16:53:49