抱歉,这是一个基本问题,我想知道为什么我的代码不需要用于 mapView 的 alloc/init。它会在保留时自动发生吗?我没有使用 ARC,并且在我的 MKMapView* mapView 的 alloc/init 上它不会导致错误,但地图视图不会显示位置信息,也不会显示为混合类型....但是在删除 alloc来自 viewDidLoad 的 /init 语句一切正常!为什么?
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface MDViewController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate>
@property (retain, nonatomic) IBOutlet MKMapView* mapView;
@end
-----
#import "MDViewController.h"
@interface MDViewController ()
{
CLLocationManager* lmanager;
}
@end
@implementation MDViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lmanager= [[CLLocationManager alloc]init];
lmanager.delegate=self;
lmanager.desiredAccuracy=kCLLocationAccuracyBest;
lmanager.distanceFilter=kCLDistanceFilterNone;
[lmanager startUpdatingLocation];
//mapView = [[MKMapView alloc]init];//without allocating here it works
mapView.delegate=self;
mapView.mapType=MKMapTypeHybrid;
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//update map
MKCoordinateSpan span;
span.latitudeDelta= .001;
span.longitudeDelta=.001;
MKCoordinateRegion region;
region.center= newLocation.coordinate;
region.span=span;
[mapView setRegion:region animated:YES];
[mapView setShowsUserLocation:YES];
}