基本的な質問で申し訳ありませんが、コードが 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];
}