1

カスタムデリゲートの初期化について質問があります。MyScrollView initWithFrameメソッド内に、デリゲートを送信する必要がある最初の位置があります。しかし、イニシャライザの後にMyCustomView内にデリゲートを設定したため、まだ不明です。

どうすればそれを修正できるので、init内でもデリゲートが呼び出されますか?ご協力いただきありがとうございます..

MyCustomView.m

 self.photoView = [[MyScrollView alloc] initWithFrame:frame withDictionary:mediaContentDict];
 self.photoView.delegate = self;
//....

MyScrollView.h
@protocol MyScrollViewDelegate
-(void) methodName:(NSString*)text;
@end
@interface MyScrollView : UIView{
 //...
    __unsafe_unretained id <MyScrollViewDelegate> delegate;
}
@property(unsafe_unretained) id <MyScrollViewDelegate> delegate;


MyScrollView.m

-(id) initWithFrame:(CGRect)frame withDictionary:(NSDictionary*)dictionary{ 
self.content = [[Content alloc] initWithDictionary:dictionary];

    self = [super initWithFrame:frame];
    if (self) {
      //.... other stuff

     // currently don´t get called
     [self.delegate methodName:@"Test delegate"];
}
return self;
}
4

2 に答える 2

4

私はあなたが定義したと確信しています:

- (id)initWithFrame:(CGRect)frame withDictionary:(NSDictionary *)dictionary;

次に、デリゲートも渡します。

- (id)initWithFrame:(CGRect)frame withDictionary:(NSDictionary *)dictionary withDelegate:(id<MyScrollViewDelegate>)del;

実装ファイル内:

- (id)initWithFrame:(CGRect)frame withDictionary:(NSDictionary *)dictionary withDelegate:(id<MyScrollViewDelegate>)del {
    // your stuff...

    self.delegate = del;
    [self.delegate methodName:@"Test delegate"];

}

これを使って:

self.photoView = [[MyScrollView alloc] initWithFrame:frame withDictionary:mediaContentDict withDelegate:self];
于 2012-07-11T19:00:24.283 に答える
1

1つのオプションは、カスタムクラスの初期化子でデリゲートを渡すことです。

-(id)initWithFrame:(CGRect)frame withDictionary:(NSDictionary*)dictionary delegate:(id)delegate 
{ 
    self = [super initWithFrame:frame];
    if (self == nil )
    {
        return nil;
    }
    self.content = [[Content alloc] initWithDictionary:dictionary];
    self.delegate = delegate;
    //.... other stuff

    // Delegate would exist now
    [self.delegate methodName:@"Test delegate"];

    return self;
}
于 2012-07-11T19:01:36.130 に答える