2016-07-17 19 views
0

私はこれを検索しましたが、うまくいかない回答を見つけることができませんでした。どのようにしてサブクラスからデリゲートメソッドに正常にアクセスできますか?サブクラスの代理人にアクセス

ので、同じように:

#import <MapKit/MapKit.h> 

@protocol customMapViewDelegate <MKMapViewDelegate> 
// 
@end 

@interface customSubClass : MKMapView <MKMapViewDelegate> 
// 
@end 

基本的に、私は私のサブクラスではなく、私がでcustomSubclassを実装していたファイルにMkMapViewデリゲートにアクセスしたい

答えて

0

あなたMKMapView.hにこれを追加します。ファイル:

@protocol MKMapViewDelegate <NSObject> 
@required 
// Put your required delegate methods here 
-(void)myFirstRequiredMethod; 

@optional 
// Pute your optional delegate methods here 

@end 

そして、あなたのcustomSubClass.mファイルにデリゲートメソッドを使用することができます。

@interface customSubClass : MKMapView <MKMapViewDelegate> 

-(void)myFirstRequiredMethod 
{ 
    NSLog(@"Hello"); 
} 

@end 

c.f.詳細については、こちらのリンク:http://www.idev101.com/code/Objective-C/delegate.html

EDIT(OPからのコメントの後) 時々、あなたは、オブジェクトのデリゲートとしてself設定する必要はありません。他のオブジェクトをデリゲートとして完全に設定できます。私はインターフェイスを宣言しない場合

@implementation MainViewController 


- (void)viewDidLoad { 

    [super viewDidLoad]; 
    self.myMKMapView.delegate = self.myCustomDelegate 
} 
} 
+0

@interface customSubClass : MKMapView <MKMapViewDelegate> { } @property (nonatomic, weak) MyCustomDelegateClass *myCustomDelegate; @property (nonatomic, strong) MKMapView *myMKMapView; @end 

customSubClass.m

@interface customDelegate : MyCustomDelegateClass <MKMapViewDelegate> -(void)myDelegateMethod { NSLog("hello") } @end 

次に、あなたのcustomSubClassはタイプcustomDelegate

customSubClass.hの性質を持っている必要があります。 hファイル、私はサブクラスとしてそれをアクセスすることはできません..だから私はクエストを推測するどのように別のView Controllerでサブクラスとしてアクセスするのですか?すべてのデリゲートをカスタムデリゲートに戻します。 –

+0

私は私の答えを編集しました。あなたが達成したいことを完全に理解しているかどうかは確かではありませんが、 – Randy

+0

それはもっと似ています!ありがとう!参照サイクルのために、委任者は常に弱く宣言されるべきではありませんか? –