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
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 a parent view controller with a single method I want available to all child classes:
#import "GAViewController.h"
#interface GAViewController ()<UITextFieldDelegate>
#end
#implementation GAViewController
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#end
I have a register view controller which looks like this:
//.h
#import <UIKit/UIKit.h>
#import "GAViewController.h"
#import "GAViewController.m"
#interface GARegisterViewController : GAViewController
#end
//.m
#import "GARegisterViewController.h"
#interface GARegisterViewController ()<UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIGestureRecognizerDelegate>
#property (weak, nonatomic) IBOutlet UIButton *registerButton;
#property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
#property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
#property (weak, nonatomic) IBOutlet UITextField *firstNameTextField;
#property (weak, nonatomic) IBOutlet UITextField *lastNameTextField;
#property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
#property (weak, nonatomic) IBOutlet UISegmentedControl *genderSegmentedContoller;
#end
#implementation GARegisterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self makeTextFieldDelegates];
}
- (void)makeTextFieldDelegates{
[self.userNameTextField setDelegate:self];
[self.passwordTextField setDelegate:self];
[self.firstNameTextField setDelegate:self];
[self.lastNameTextField setDelegate:self];
[self.phoneNumberTextField setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is the error I receive:
Does anyone know I can either fix the error above? Or create a parent class correctly with the textFieldShoudlReturn method so that I don't have to include in all my views.
Thanks!
I am importing .m because it contains the textfieldshouldreturn method, it also imports the .h file
It is importing .m file that causes duplicate symbols. Importing a .m file causes the file from which it is imported to define the same symbols (such as method implementations and functions / variables with external scope) as the .m file being included, causing the duplication.
For the same reason one should never place #implementation blocks into header files.
In order to fix this, make a header for GAViewController, and declare textFieldShouldReturn: inside it. Remove #import "GAViewController.m" from your headers and .m files, replacing with #import "GAViewController.h". This should fix the problem.
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;
}
I'm trying to creating a custom UIView, lets call it FooView.
FooView.h
#import <UIKit/UIKit.h>
#interface FooView : UIView
#property (nonatomic, strong) UITextField *barTextField;
#property (nonatomic, strong) UIButton *submitButton;
#end
FooView.m
#import "FooView.h"
#implementation FooView
#synthesize barTextField = _barTextField;
#synthesize submitButton = _submitButton;
...
#end
FooViewController.h
#import <UIKit/UIKit.h>
#import "FooView.h"
#interface FooViewController : UIViewController
#property (nonatomic, strong) FooView *fooView;
#end
FooViewController.m
#import "SearchViewController.h"
#interface SearchViewController ()
#end
#implementation SearchViewController
#synthesize fooView = _fooView;
#end
I want the button touch event to be implemented in FooViewController, is delegate can achieve this? If YES, how?
Currently, I'm adding the touch event in such a way
FooViewController.m
- (void)viewDidLoad
{
[self.fooView.submitButton addTarget:self action:#selector(submitTapped:) forControlEvents:UIControlEventTouchUpInside];
}
...
- (IBAction)submitTapped
{
...
}
But I don't think this is a good solution, so need some expert advice.
Any idea?
Yes you can implement using delegate
FooView.h
#import <UIKit/UIKit.h>
#protocol FooViewDelegate
-(void)submitButtonClicked:(id)sender;
#end
#interface FooView : UIView
#property (nonatomic, strong) UITextField *barTextField;
#property (nonatomic, strong) UIButton *submitButton;
#property (nonatomic, assign) id<FooViewDelegate> delegate;
#end
FooView.m
#import "FooView.h"
#implementation FooView
#synthesize barTextField = _barTextField;
#synthesize submitButton = _submitButton;
#synthesize delegate;
...
-(IBAction)buttonClicked:(id)sender // connect this method with your button
{
[self.delegate submitButtonClicked:sender];
}
#end
FooViewController.h
#import <UIKit/UIKit.h>
#import "FooView.h"
#interface FooViewController : UIViewController <FooViewDelegate>
#property (nonatomic, strong) FooView *fooView;
#end
FooViewController.m
#import "FooViewController.h"
#interface FooViewController ()
#end
#implementation FooViewController
#synthesize fooView = _fooView;
- (void)viewDidLoad
{
_fooView = [[UIView alloc] init];
_fooView.delegate = self;
}
-(void)submitButtonClicked:(id)sender //delegate method implementation
{
NSLog(#"%#",[sender tag]);
}
#end
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