【发布时间】:2010-11-25 06:01:27
【问题描述】:
我将一组非常复杂的 Web 服务和搜索归结为以下简单的代码。 我需要能够向地图添加注释以响应搜索(或在下面的示例中单击按钮),然后允许用户再次单击按钮并获得一组新结果。实际上会有一个不同的数字,但在简化的示例中,我们总是向地图视图添加一个注释。 我相信我的代码应该删除现有的注释并添加一个新的注释,但是在第二次和随后的按钮按下时它会泄漏 32 个字节。 我错过了什么? (或视情况保留!)
testViewController.h
#importtestViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil 包:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // 自定义初始化 self.title=@"测试"; } 回归自我; } - (void) storeLocationInfo: (CLLocationCoordinate2D) loc title:(NSString *)t subtitle:(NSString *)st index:(int)i { NSArray * 注释 = [mapView 注释]; [mapView removeAnnotations:annotations]; MyMark * mymark=[[MyMark alloc] initWithCoordinate:loc]; [mapView addAnnotation:mymark]; [MyMark 发布]; } - (void)viewDidLoad { [超级视图DidLoad]; UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"添加点到地图" style:UIBarButtonItemStylePlain target:self action:@selector(addPushed)]; [self.navigationItem setRightBarButtonItem:barButton]; [barButton 释放]; mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)]; mapView.showsUserLocation=假; mapView.delegate=自己; [self.view insertSubview:mapView atIndex:0]; [地图视图发布]; } -(无效)addPushed { MKCoordinateRegion reg = mapView.region; [self storeLocationInfo:reg.center title:@"price" subtitle:@"title" index:1]; } - (无效)dealloc { [超级释放]; }MyMark.h
#importMyMark.m
#import "MyMark.h" @implementation MyMark @synthesize 坐标、索引; @synthesize 标题,副标题; -(id)initWithCoordinate:(CLLocationCoordinate2D) c{ 坐标=c; NSLog(@"%f,%f",c.latitude,c.longitude); 回归自我; } -(id)setCoordinate:(CLLocationCoordinate2D) c{ 坐标=c; NSLog(@"%f,%f",c.latitude,c.longitude); 回归自我; } -(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int) i{ self.title=t; self.subtitle=st; 指数=我; 回归自我; } -(无效)dealloc { [标题发布]; [字幕发布]; [超级释放]; }【问题讨论】:
标签: ios objective-c cocoa-touch memory-leaks mapkit