2

EDITED私は.xibファイルを持っていますUIView. UIViewいくつかの要素とタップ ジェスチャ認識エンジンが含まれています。

AnnotationViewコンセントとつながっています。ではAnnotationView、タップ イベントをキャッチしており、正常に動作しています。

ViewController次に、このイベントを親ビュー ( )に渡そうとしています。ここで、AnnotationViewインスタンスが作成され、サブビューとして追加されました。しかし、デリゲート メソッドが呼び出されません。この問題を解決する方法がわかりません。サブビューを使用しているためなのか、デリゲートで何か問題を起こしているだけなのかわかりません。

ここにいくつかのコードがあります:

AnnotationView.h

#import <UIKit/UIKit.h>

@protocol protName <NSObject>

-(void) test;

@end

@interface AnnotationView : UIView

@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UILabel *accessoryLabel;
@property (strong, nonatomic) IBOutlet UITapGestureRecognizer *tapRecognizer;
@property (nonatomic, weak) id <protName> delegate;

-(IBAction)handleTap:(UITapGestureRecognizer *)tapRecognizer;
@end

AnnotationView.m

#import "AnnotationView.h"

@implementation AnnotationView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"AnnotationView"
                                                             owner:self
                                                           options:nil];
        AnnotationView *_myView = [nibContents objectAtIndex:0];
        _myView.backgroundColor = [UIColor greenColor];
        [self addSubview: _myView];

    }
    return self;
 }

-(IBAction)handleTap:(UITapGestureRecognizer *)tapRecognizer{
    NSLog(@"tap tap");

    [self.delegate test]; //this delegate is nil
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "AnnotationView.h"

@interface ViewController : UIViewController <protName>
@property (nonatomic,retain) AnnotationView *annot;

-(void) test;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    _annot = [[AnnotationView alloc] initWithFrame:CGRectMake(100, 100, 212, 45)];
    _annot.textLabel.text = @"some text";
    _annot.delegate = self;
    [self.view addSubview:_annot];
}
-(void) viewWillAppear:(BOOL)animated{
    _annot.delegate = self;
}
-(void) test{
    NSLog(@"Delegate massage is received!");
}
@end
4

3 に答える 3

1

なぜこれをしないのですか:

@implementation ViewController

- (void)viewDidLoad
{
    CGRect frame = CGRectMake(100, 100, 212, 45)];
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"AnnotationView"
                                                         owner:nil
                                                       options:nil];
    for (id object in nibContents) {
        if ([object isKindOfClass:[AnnotationView class]]) {
            _annot = (AnnotationView *)object;
        }
    }        
    _annot.frame = frame;
    _annot.backgroundColor = [UIColor greenColor];
    _annot.textLabel.text = @"some text";
    _annot.delegate = self;
    [self.view addSubview:_annot];
}
于 2013-08-08T13:33:42.603 に答える
1

デリゲートの正しい使い方!

SecondViewController.h で:

   @protocol messageDelegate <NSObject>
    @optional
    -(void) test;
    @end
    @interface SecondViewController : NSString
    @property (nonatomic, assign) id <messageDelegate> delegate;
    @end

SecondViewController.m で:

-(void)readyToSend
{
    [self.delegate test];

}

ViewController.h で:

@interface ViewController : UIViewController<messageDelegate>
@end

ViewController.m: で

- (void)viewDidLoad {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.delegate = self;
}
-(void) test{
NSLog(@"Delegate massage is received!");

}

それが役立つことを願っています!

于 2013-08-08T13:12:40.610 に答える
0

プロトコルを実装するのは AnnotationView ではなく、viewController です。したがって、次のように移動できます。

@interface AnnotationView : UIView <protName>
于 2013-08-08T13:13:59.837 に答える