【问题标题】:initialization and allocation without arc?没有弧的初始化和分配?
【发布时间】:2012-05-25 22:10:52
【问题描述】:

抱歉,这是一个基本问题,我想知道为什么我的代码不需要用于 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];

}

【问题讨论】:

    标签: ios mkmapview mapkit


    【解决方案1】:

    您不需要分配初始化您的地图视图,因为它是由 Xib 为您完成的。加载界面 xib 时,框架会看到您有一个冻结的 mapview 并自动分配初始化它,然后将该 mapview 分配给您的 viewcontroller 代码中的那个。 如果在您的代码中分配 init,则会断开两者之间的联系。

    使其工作的一种方法是在您的 IB xib 中没有 mapview 并分配初始化它,setDelegate,设置框架,最后将其视图添加为主视图的子视图。

    我尽量保持简洁。我希望我们清楚你。 ARC与这一切没有任何关系。

    【讨论】:

    • 非常感谢您的清晰解释。它消除了我的困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2011-12-24
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    相关资源
    最近更新 更多