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
答案 0 :(得分: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!");
}
希望它会有所帮助!
答案 1 :(得分: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];
}
答案 2 :(得分:0)
您的viewController不是实现协议的AnnotationView。 所以你可以移动它给:
@interface AnnotationView : UIView <protName>