0
對不起,它的一個基本問題,我想知道爲什麼我的代碼不需要mapView的alloc/init。它會在保留時自動發生嗎?我沒有使用ARC和我的MKMapView * mapView的alloc/init它不會導致錯誤,但地圖視圖不顯示位置信息,也不會顯示爲混合類型....但在移除alloc/init的聲明來viewDidLoad它的作品都很好!爲什麼?初始化和分配無弧?
#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];
}
非常感謝您的明確解釋。它清除了我的困惑。 – sani