rory rory - 5 months ago 13
iOS Question

Delegate in Custom Cell

Hello everyone I'm trying to work with a custom delegate in my cell ...

In my file.h the custom cell I entered this:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@class FFCustomCellWithImage;
@protocol FFCustomCellWithImageDelegate

- (void) customCell:(FFCustomCellWithImage *)cell button1Pressed:(UIButton *)btn;

@end


@interface FFCustomCellWithImage : UITableViewCell

@property (nonatomic, assign) id<FFCustomCellWithImageDelegate> delegate;

@property (strong, nonatomic) IBOutlet PFImageView *FotoPost;
@property (strong, nonatomic) IBOutlet PFImageView *FotoProfilo;
@property (strong, nonatomic) IBOutlet UILabel *NomeUtente;
@property (strong, nonatomic) IBOutlet UILabel *TestoPost;
@property (strong, nonatomic) IBOutlet UILabel *DataPost;
@property (strong, nonatomic) IBOutlet UIView *backgroundCell;
@property (strong, nonatomic) IBOutlet UIButton *LeggiCommentoButton;
@property (strong, nonatomic) IBOutlet UIButton *AddGoPoint;
@property (strong, nonatomic) IBOutlet UILabel *CountGoPoint;



@end


and in my file.m the custom cell I entered this

#import "FFCustomCellWithImage.h"
#import <Parse/Parse.h>

@implementation FFCustomCellWithImage
@synthesize delegate;


-(void)buttonPressed{


if (delegate && [delegate respondsToSelector:@selector(customCell:button1Pressed:)]) {
[delegate customCell:self button1Pressed:self.AddGoPoint];
}
}


the problem is that it gives me an error saying that respondsToSelector
Know no instance method

Where is my mistake? sorry but I'm new to the delegates and am trying to learn

Answer

The problem is because you declared your @protocol wrong, it should be:

@protocol FFCustomCellWithImageDelegate <NSObject>

- (void) customCell:(FFCustomCellWithImage *)cell button1Pressed:(UIButton *)btn;

@end

You were not giving it a super type of NSObject, therefore its not aware that respondsToSelector is a method of the FFCustomCellWithImageDelegate instance.

Also, if you are using ARC, your delegate property should be declared as (weak, nonatomic) you can read more about it here if need be (The post wasn't originally to explain that, but it contains it): Why are Objective-C delegates usually given the property assign instead of retain?