我已经将一组非常复杂的 Web 服务和搜索归结为以下简单的代码。我需要能够向地图添加注释以响应搜索(或在下面的示例中单击按钮),然后允许用户再次单击按钮并获得一组新结果。实际上会有一个不同的数字,但在简化的示例中,我们总是向地图视图添加一个注释。我相信我的代码应该删除现有的注释并添加一个新的注释,但是在第二次和随后的按钮按下时它会泄漏 32 个字节。我错过了什么?(或视情况保留!)
测试视图控制器.h
#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import "MyMark.h" @interface testViewController : UIViewController { MKMapView *mapView; } @结尾
测试视图控制器.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
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyMark : NSObject<MKAnnotation> { CLLocationCoordinate2D坐标; NSString * 标题; NSString * 字幕; 整数索引; } @property (nonatomic, readonly) CLLocationCoordinate2D 坐标; @property (nonatomic, readonly) int index; @property (nonatomic, 保留) NSString * title; @property (nonatomic, 保留) NSString * subtitle; -(id)initWithCoordinate:(CLLocationCoordinate2D) 坐标; -(id)setCoordinate:(CLLocationCoordinate2D) 坐标; -(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int)i ; @结尾
MyMark.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 { [标题发布]; [字幕发布]; [超级释放]; }