非常に複雑な一連の Web サービスと検索を煮詰めて、次の単純なコードに落とし込みました。検索に応答して (または下のサンプルではボタンをクリックして) 注釈をマップに追加し、ユーザーがボタンをもう一度クリックして新しい結果セットを取得できるようにする必要があります。実際には異なる数がありますが、単純化した例では、常に 1 つの注釈をマップ ビューに追加します。コードで既存の注釈を削除して新しい注釈を追加する必要があると思いますが、2 回目以降のボタンの押下で 32 バイトがリークします。私は何が欠けていますか? (または、場合によっては保持してください!)
testViewController.h
#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import "MyMark.h" @interface testViewController : UIViewController { MKMapView *mapView; } @終わり
testViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil バンドル:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // カスタム初期化 self.title=@"テスト"; } 自分自身を返します。 } - (void) storeLocationInfo: (CLLocationCoordinate2D) loc タイトル:(NSString *)t サブタイトル:(NSString *)st インデックス:(int)i { NSArray * 注釈 = [mapView 注釈]; [mapView removeAnnotations:注釈]; MyMark * mymark=[[MyMark alloc] initWithCoordinate:loc]; [mapView addAnnotation:mymark]; [MyMark リリース]; } - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"マップにポイントを追加" style:UIBarButtonItemStylePlain target:self action:@selector(addPushed)]; [self.navigationItem setRightBarButtonItem:barButton]; [バーボタンを離す]; mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)]; mapView.showsUserLocation=FALSE; mapView.delegate=self; [self.view insertSubview:mapView atIndex:0]; [mapView リリース]; } - (無効) addPushed { MKCoordinateRegion reg = mapView.region; [セルフ storeLocationInfo:reg.center タイトル:@"価格" サブタイトル:@"タイトル" インデックス:1]; } - (無効)dealloc { [スーパーdealloc]; }
MyMark.h
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyMark : NSObject<MKAnnotation> { CLLocationCoordinate2D 座標; NSString * タイトル; NSString * サブタイトル。 int インデックス; } @property (非アトミック、読み取り専用) CLLocationCoordinate2D 座標。 @property (非アトミック、読み取り専用) int インデックス。 @property (非アトミック、保持) NSString * タイトル; @property (非アトミック、保持) NSString * サブタイトル。 -(id)initWithCoordinate:(CLLocationCoordinate2D)座標; -(id)setCoordinate:(CLLocationCoordinate2D)座標; -(id)setTitle:(NSString *)t 字幕:(NSString *)st index:(int)i ; @終わり
MyMark.m
#import "MyMark.h" @実装 MyMark @synthesize 座標、インデックス; @synthesize タイトル、サブタイトル; -(id)initWithCoordinate:(CLLocationCoordinate2D) c{ 座標=c; NSLog(@"%f,%f",c.緯度,c.経度); 自分自身を返します。 } -(id)setCoordinate:(CLLocationCoordinate2D) c{ 座標=c; NSLog(@"%f,%f",c.緯度,c.経度); 自分自身を返します。 } -(id)setTitle:(NSString *)t 字幕:(NSString *)st index:(int) i{ self.title=t; self.subtitle=st; インデックス=私; 自分自身を返します。 } -(void) 割り当て解除 { [タイトルリリース]; 【字幕公開】; [スーパーdealloc]; }