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
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?
If you are using ARC, you'd better change
#property (nonatomic, assign) id<FFCustomCellWithImageDelegate> delegate;
to
#property (nonatomic, weak) id<FFCustomCellWithImageDelegate> delegate;
and method
-(void)buttonPressed{
if (delegate && [delegate respondsToSelector:#selector(customCell:button1Pressed:)]) {
[delegate customCell:self button1Pressed:self.AddGoPoint];
}
}
to
-(void)buttonPressed{
if (self.delegate && [self.delegate respondsToSelector:#selector(customCell:button1Pressed:)]) {
[self.delegate customCell:self button1Pressed:self.AddGoPoint];
}
}
and
#class FFCustomCellWithImage;
#protocol FFCustomCellWithImageDelegate
to
#class FFCustomCellWithImage;
#protocol FFCustomCellWithImageDelegate <NSObject>
In UITableView Datasource method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
FFCustomCellWithImage *cell = (FFCustomCellWithImage *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[FFCustomCellWithImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// Make sure you have this code
cell.delegate = self;
}
return cell;
}
Related
these 2 are the files where i m creating a protocol and then declare the delegate in another class
this is my favouriteViewController.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "ViewController.h"
#class FavouritesTableViewController;
#protocol FavouritesTableViewControllerDelegate<NSObject>
- (void)senDetailsViewController:(FavouritesTableViewController *)controller didFinishEnteringItem:(NSArray *)item;
#end
#interface FavouritesTableViewController : UITableViewController <UISearchControllerDelegate,UISearchBarDelegate>
#property (strong, nonatomic) IBOutlet UISearchController *search;
#property (strong, nonatomic) IBOutlet UITableView *table;
#property (nonatomic, weak) id < FavouritesTableViewControllerDelegate > delegate;
#end
and this is my viewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "FavouritesTableViewController.h"
#interface ViewController : UIViewController <CLLocationManagerDelegate,FavouritesTableViewControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *weatherIcon;
#property (weak, nonatomic) IBOutlet UILabel *Place;
#property (weak, nonatomic) IBOutlet UILabel *Temperature;
#property (weak, nonatomic) IBOutlet UILabel *unit;
#property (weak, nonatomic) IBOutlet UILabel *weatherText;
#property (weak, nonatomic) IBOutlet UITextView *Info;
#property (weak, nonatomic) IBOutlet UILabel *summary;
#property (strong,nonatomic) NSString *longitude;
#property (strong,nonatomic) NSString *latitude;
#property (strong,nonatomic) NSString *locationName;
#property BOOL setLocation;
#property (weak, nonatomic) IBOutlet UIScrollView *scroll;
- (IBAction)forecast:(UIButton *)sender;
- (IBAction)Share:(UIButton *)sender;
- (IBAction)history:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activIndicator;
- (IBAction)favbutton:(id)sender;
#end
the error i get is
:- Cannot find protocol declaration for
'FavouritesTableViewControllerDelegate'
I'm declaring these methods and protocols to pass data from FavouriteViewController to ViewController
and this is the protocol method which i call in ViewController.m
-(void)senDetailsViewController:(FavouritesTableViewController *)controller didFinishEnteringItem:(NSArray *)item
{
controller.delegate = self;
self.latitude = [item[0] valueForKey:#"lat"];
self.longitude = [item[0] valueForKey:#"long"];
self.locationName = [item[0] valueForKey:#"name"];
self.setLocation = YES;
[self viewDidLoad];
}
Ths is happening because of recursive import, in FavouritesTableViewController you are importing "ViewController.h" and again ViewController.h you are importing "FavouritesTableViewController.h"
try
#class viewController;
#class FavouritesTableViewController;
in FavouritesTableViewController.h and remove "#import ViewController.h"
I have created delegate method and working in Xcode lower version but not working in Xcode 6.1.
Its showing error Cannot Find protocol declaration NSObject
Tried code:
.h file
#class ReportCell;
#protocol keyboardDelegate <NSObject>
#optional
- (BOOL)leaveKeyboard:(ReportCell *)cell ;
#end
#import <UIKit/UIKit.h>
#interface ReportCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *imgReport;
#property (strong, nonatomic) IBOutlet UITextField *txtReport;
#property (strong, nonatomic) IBOutlet UIView *viewReport;
#property (nonatomic, assign) id <keyboardDelegate> delegate;
#end
.m file
#import "ReportCell.h"
#implementation ReportCell
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_delegate leaveKeyboard:self];
[self.superview endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
Hey You can solve you problem by following change in your .h file.
#import <UIKit/UIKit.h>
#class ReportCell;
#protocol keyboardDelegate;
#interface ReportCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *imgReport;
#property (strong, nonatomic) IBOutlet UITextField *txtReport;
#property (strong, nonatomic) IBOutlet UIView *viewReport;
#property (nonatomic, assign) id <keyboardDelegate> delegate;
#end
#protocol keyboardDelegate <NSObject>
#optional
- (BOOL)leaveKeyboard:(ReportCell *)cell ;
#end
Here InformacionViewController.h:
#import <UIKit/UIKit.h>
#import "LibrosFenomenales.h"
#interface InformacionViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *nombre;
#property (strong, nonatomic) IBOutlet UILabel *autor;
#property (strong, nonatomic) IBOutlet UILabel *año;
#property (strong, nonatomic) IBOutlet UILabel *genero;
#property (strong, nonatomic) IBOutlet UITextView *argumento;
#property (strong, nonatomic) IBOutlet UIImageView *portada;
#property LibrosFenomenales *libroSeleccionado;
#end
Here ViewController.m:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
InformacionViewController *informacionViewController = [self.storyboard
instantiateViewControllerWithIdentifier:#"InformacionViewController"];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:informacionViewController];
informacionViewController.libroSeleccionado = [_libros objectAtIndex:indexPath.row];
[self presentViewController:navigationController animated:YES completion:nil];
}
Xcode shows me this error: Property "libroSeleccionado" not found on object of type "InformacionViewController".
This is the line: informacionViewController.libroSeleccionado = [_libros objectAtIndex:indexPath.row];
What am i doing wrong?
Thanks
Instead of Quiting the Xcode you just First clean the build. To clean the build Press command+Shift+K. then run the application even then If it doesn't work for then try this #property (strong, nonatomic) LibrosFenomenales *libroSeleccionado;
instead of #property LibrosFenomenales *libroSeleccionado;
Hard to say without having access to the whole code, but try these:
If "LibrosFenomenales" is an Objective-C class, change the property declaration to:
#property (strong, nonatomic) LibrosFenomenales *libroSeleccionado;
If the LibrosFenomenales.h header imports the InformacionViewController.h header, you have a cross-referencing issue. To fix this, open up InformacionViewController.h and replace:
#import "LibrosFenomenales.h"
by:
#class LibrosFenomenales;;
then open up InformacionViewController.m and add the #import "LibrosFenomenales.h" to it.
Don't forget to #import "InformacionViewController.m" in ViewController.m
I am trying to use Delegates to communicate thru view controllers. Here is my code:
#protocol ViewControllerDelegate;
#interface ViewController : UIViewController <UITextFieldDelegate>{
IBOutlet UIDatePicker *dateTimePicker;
}
#property (nonatomic, strong) IBOutlet UITextField *className;
#property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
#property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
#property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
#property (nonatomic, strong) IBOutlet UILabel *notificationOnOffLabel;
#property (nonatomic, assign) id <ViewControllerDelegate> delegate;
#property (nonatomic,strong)classObject *classObject;
-(IBAction) addButton:(id)sender;
#end
#protocol ViewControllerDelegate <NSObject>
-(void)assignmentSaved:(classObject *)classObj;
In action:
[self.delegate assignmentSaved:self.classObject];
In other view controller:
-(ViewController *)vc
{
if (!_vc) {
_vc = [[ViewController alloc]init];
}
return _vc;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.vc.delegate = self;
}
-(void)assignmentSaved:(classObject *)classObj
{
NSLog(#"%#",classObj.description);
classObj2 = classObj;
[self.tableView reloadData];
}
vc is a property of View Controller. When I NSLog the delegate, the delegate ends up to be nil. Also I tried #mialkan answer yet it still does not work. If you need any more info just ask.
I made some changes in your code, and dont forget to #synthesize delegate; and dont for get to setDelegate:self where you create ViewController.
#protocol ViewControllerDelegate;
#interface ViewController : UIViewController <UITextFieldDelegate>{
IBOutlet UIDatePicker *dateTimePicker;
id <ViewControllerDelegate> delegate;
}
#property (nonatomic, strong) IBOutlet UITextField *className;
#property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
#property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
#property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
#property (nonatomic, strong) IBOutlet UILabel *notificationOnOffLabel;
#property (nonatomic, assign) id <ViewControllerDelegate> delegate;
#property (nonatomic,strong)classObject *classObject;
-(IBAction) addButton:(id)sender;
#end
#protocol ViewControllerDelegate
-(void)assignmentSaved:(classObject *)classObj;
#end
In the first view did you subscribe to your delegate < ViewControllerDelegate > ?
MyOtherViewController : UIViewController <ViewControllerDelegate>
and you can only do that :
- (void)viewDidLoad
{
[super viewDidLoad];
if (!_vc)
_vc = [[ViewController alloc]init];
self.vc.delegate = self;
}
I've got 2 views. The firstView and the secondView. The firstView needs the favourites array from the secondView, so I try to call the getFavourites method defined in the protocol. This returns null however, which seems strange to me because everything makes sense.
Here is the firstViewController.h:
#import <UIKit/UIKit.h>
#import "Drink.h"
#protocol firstViewControllerDelegate;
#interface FirstViewController : UIViewController
{
id <firstViewControllerDelegate> delegate;
NSMutableArray *labels;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *button4;
}
- (IBAction) buttonClick: (id) sender;
#property (nonatomic, assign) id <firstViewControllerDelegate> delegate;
#property (nonatomic, retain) IBOutlet UIButton *button1;
#property (nonatomic, retain) IBOutlet UIButton *button2;
#property (nonatomic, retain) IBOutlet UIButton *button3;
#property (nonatomic, retain) IBOutlet UIButton *button4;
#end
#protocol firstViewControllerDelegate
- (NSMutableArray *) getFavourites;
- (void) setFavourites: NSMutableArray;
#end
firstViewController.m
#synthesize delegate;
.......
- (void) viewWillAppear:(BOOL)animated
{
NSMutableArray *favourites = [delegate getFavourites]; // favourites is empty after this line
[button1 setTitle:[[favourites objectAtIndex:1] name] forState:UIControlStateNormal];
NSLog(#"VIEW APPEARED. BUTTON TITLE IS: %#", button1.currentTitle);
}
SecondViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#interface SecondViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, firstViewControllerDelegate>
{
NSMutableArray *favourites;
NSMutableArray *drinks;
}
#property (nonatomic, retain) NSMutableArray *drinks;
#property (nonatomic, retain, getter = getFavourites) NSMutableArray *favourites;
- (void) addDrink: (NSString *) name;
#end
Does anybody have any idea why this isn't working?
Are you sure you're setting the delegate property of FirstViewController? i.e.:
FirstViewController *firstController = // Instantiate the controller;
firstController.delegate = secondController;
[self.navigationController pushViewController:firstController animated:YES];
secondController should exist before instantiation, otherwise you're setting a nil value