MapKit の別のデリゲート クラスをセットアップする適切な方法は何ですか?
MKMapView をサブクラス化する MapView クラスと、初期化メソッドが 1 つしかない MKMapViewDelegate プロトコルに準拠する裸の MapDelegate クラスがあります。
以下は、私が使用する MapView 初期化メソッドからの抜粋です。
# MapView.m ...
@implementation MapView
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// [self setShowsUserLocation:YES];
[self setDelegate:[[MapDelegate alloc] initWithMapView:self]];
MapDelegate クラスが持つ唯一のメソッドは
# MapDelegate.m ...
- (id)initWithMapView:(MapView *)aMapView {
self = [super init];
self.mapView = aMapView;
return self;
}
[self setShowsUserLocation :YES] を持っています。コメント、すべて正常に動作します-マップが表示されます。この行のコメントを外すと、アプリケーションがクラッシュし始めます。
私の MapDelegate クラスには何が欠けていますか?
更新 1: 別のクラス MapDelegate を使用せず、setDelegate:self だけを設定すると、すべて機能します。
更新 2: [self setDelegate:[[MapDelegate alloc] initWithMapView:self]];の問題がわかりました。文字列は、MapDelegate クラスが現在よりも長く存続する必要があることです (delegate プロパティには弱い属性があります)。私が次のことをした場合:
@property (strong) id delegateContainer;
....
[self setDelegateContainer:[[MapDelegate alloc] init]];
[self setDelegate:self.delegateContainer];
...できます!MKMapView のライフ サイクルと共に MapDelegate のライフ サイクルを保持するより良い方法はありますか?
ありがとう!