Reputation: 23
EDITED
I have .xib file with UIView
. UIView
contains some elements and tap gesture recognizer.
It is connected to AnnotationView
with outlets. In AnnotationView
, I am catching tap events and it works fine.
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.
Here is some code:
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
Upvotes: 2
Views: 814
Reputation: 9035
Why not just do this:
@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];
}
Upvotes: 1
Reputation: 1399
It is your viewController not the AnnotationView that implements the protocol. So you could move that to give:
@interface AnnotationView : UIView <protName>
Upvotes: 0
Reputation: 408
How to use delegate in correct way!
In SecondViewController.h:
@protocol messageDelegate <NSObject>
@optional
-(void) test;
@end
@interface SecondViewController : NSString
@property (nonatomic, assign) id <messageDelegate> delegate;
@end
In SecondViewController.m:
-(void)readyToSend
{
[self.delegate test];
}
In ViewController.h:
@interface ViewController : UIViewController<messageDelegate>
@end
In ViewController.m: in
- (void)viewDidLoad {
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.delegate = self;
}
-(void) test{
NSLog(@"Delegate massage is received!");
}
Hope it will help!
Upvotes: 1