不调用委托方法(无)

编辑我有与UIView .xib文件。 UIView包含一些元素和轻击手势识别器。

它连接到带有sockets的AnnotationView 。 在AnnotationView ,我捕捉轻拍事件,它工作正常。

然后我试图将这个事件传递给我的父视图( ViewController ), AnnotationView实例被创build并添加为子视图。 但是我的委托方法没有被调用。 我不知道,如何解决这个问题,我不知道是因为我使用的是子视图,或者我只是在委托做错了事情。

这里是一些代码:

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 

为什么不这样做:

 @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]; } 

如何正确使用委托!

在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中:in

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

}

希望它会帮助!

它是你的viewController而不是实现协议的AnnotationView。 所以你可以移动它给:

 @interface AnnotationView : UIView <protName>