问题描述
已编辑我有带有 UIView
的 .xib 文件.UIView
包含一些元素和点击手势识别器.
EDITEDI have .xib file with UIView
. UIView
contains some elements and tap gesture recognizer.
它通过插座连接到 AnnotationView
.在 AnnotationView
中,我正在捕捉点击事件并且它工作正常.
It is connected to AnnotationView
with outlets. In AnnotationView
, I am catching tap events and it works fine.
然后我试图将此事件传递给我的父视图 (ViewController
),其中 AnnotationView
实例被创建并添加为子视图.但是我的委托方法没有被调用.我不知道,如何解决这个问题,也不知道是因为我在使用子视图还是我只是在委托上做错了什么.
Then I am trying to pass this event to my parent view (ViewController
), where AnnotationView
instance was created and added as subview. But my delegate method is not getting called. I don't know, how to solve this problem, and I don't know if it is because I am using subview or I am just doing something wrong with delegate.
这是一些代码:
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];
}
这篇关于不调用委托方法(为零)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!