2011-10-04 25 views
0

我下面的大書呆子牧場指導書,並修改了我的應用程序delegate.h看起來像這樣:的MKMapView didUpdateUserLocation不會被調用

#import <UIKit/UIKit.h> 
    #import <CoreLocation/CoreLocation.h> 
    #import <MapKit/MapKit.h> 
    @interface WhereamiAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate, MKMapViewDelegate> 
    { 
     IBOutlet UITextField *locationTitleField; 
     IBOutlet UIActivityIndicatorView *activityIndicator; 
     IBOutlet MKMapView *worldView; 
     CLLocationManager *locationManager; 
    } 
    @property (nonatomic, retain) IBOutlet UIWindow *window; 
    @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; 
    @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; 
    @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; 
    - (void)saveContext; 
    - (NSURL *)applicationDocumentsDirectory; 
@end 

的.M是這樣的:

#import "WhereamiAppDelegate.h" 
@implementation WhereamiAppDelegate 
@synthesize window = _window; 
@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { 

    CLLocationCoordinate2D loc = [userLocation coordinate]; 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250); 
    [worldView setRegion:region animated:YES]; 
    NSLog(@"didUpdateUserLocation is called"); 
    } 
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
    // Override point for customization after application launch. 

// Create location manager object 
    locationManager = [[CLLocationManager alloc] init]; 
// There will be a warning from this line of code; ignore it for now 
    [locationManager setDelegate:self]; 
// We want all results from the location manager 
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
// And we want it to be as accurate as possible 
// regardless of how much time/power it takes 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
// Tell our manager to start looking for its location immediately 
// [locationManager startUpdatingLocation]; 
    [worldView setShowsUserLocation:YES]; 
// This line may say self.window, don't worry about that 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
     NSLog(@"%@", newLocation); 
} 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
     NSLog(@"Could not find location: %@", error); 
} 

當我打開應用程序時,它應該放大到我的位置。但它不縮放,所以我把一個NSLog放在didUpdateUserLocation中,看它是否被調用。但它從未打印過,所以沒有打電話。我該如何解決?

+0

注意我用的XCODE 4.1和4.3模擬器 – user973985

+0

沒關係......我發現這是因爲我忘了設定的委託! – user973985

+2

其實我並沒有忘記......書籍〜! – user973985

回答

4

同樣的事情發生在我身上......

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    ... 
    [worldView setDelegate:self]; 
    ... 
} 

THKS