I have custom UIView class GestureView. I have a forward declaration for this class and it's delegate below. I have imported GestureView.h in .m file. This works fine but iOS gives warning message saying "Cannot find protocol definition for GestureViewDelegate". If I remove forward declaration it gives same warning message as error. I don't want to import GestureView.h from ContainerViewController.h as I usually imports stuffs in .m file. Could someone please explain what's wrong in following class structure?
ContainerViewController.h
#import <UIKit/UIKit.h>
#class DividerView;
#class GestureView;
#protocol GestureViewDelegate;
#interface ContainerViewController : UIViewController<GestureViewDelegate>
#property (strong, nonatomic) IBOutlet GestureView *topContentView;
#end
GestureView.h
#import <UIKit/UIKit.h>
#protocol GestureViewDelegate;
#interface GestureView : UIView
- (void)initialiseGestures:(id)delegate;
#end
#protocol GestureViewDelegate <NSObject>
#required
- (void)GestureView:(GestureView*)view handleSignleTap:(UITapGestureRecognizer*)recognizer;
#end
I like that you're trying to avoid imports in header files: very good practice. However, to fix your bug you can just make your code even better! In my opinion it's not really necessary that your ContainerViewController class outwardly declares that it supports GestureViewDelegate protocol, so you should move this into your implementation file. Like so:
GestureView.h
#import <UIKit/UIKit.h>
#protocol GestureViewDelegate;
#interface GestureView : UIView
- (void)initialiseGestures:(id <GestureViewDelegate>)delegate;
#end
#protocol GestureViewDelegate <NSObject>
#required
- (void)gestureView:(GestureView *)view handleSingleTap:(UITapGestureRecognizer *)recognizer;
#end
ContainerViewController.h
#import <UIKit/UIKit.h>
#class GestureView;
#interface CollectionViewController : UIViewController
// this property is declared as readonly because external classes don't need to modify the value (I guessed seen as it was an IBOutlet)
#property (strong, nonatomic, readonly) GestureView *topContentView;
#end
ContainerViewController.m
#import "ContainerViewController.h"
#import "GestureView.h"
// this private interface declares that GestureViewDelegate is supported
#interface CollectionViewController () <GestureViewDelegate>
// the view is redeclared in the implementation file as readwrite and IBOutlet
#property (strong, nonatomic) IBOutlet GestureView *topContentView;
#end
#implementation ContainerViewController
// your implementation code goes here
#end
Try this way, and please reply if it works or not.
GestureView.h
#import <UIKit/UIKit.h>
#protocol GestureViewDelegate <NSObject>
#required
- (void)GestureView:(GestureView*)view handleSignleTap:(UITapGestureRecognizer*)recognizer;
#end
#interface GestureView : UIView
- (void)initialiseGestures:(id)delegate;
#end
ContainerView.h
#import <UIKit/UIKit.h>
#class DividerView;
#class GestureView;
/*#protocol GestureViewDelegate;*/ //NO NEED TO WRITE THIS
#interface ContainerViewController : UIViewController<GestureViewDelegate>
#property (strong, nonatomic) IBOutlet GestureView *topContentView;
#end
Related
i try to define simple private methods and properties in class but it gives me:
Duplicate interface definition for class 'FBViewController'
this is the .m file :
#import "FBAppDelegate.h"
#import "FBViewController.h"
#import<FacebookSDK/FacebookSDK.h>
#import "FBLoginViewController.h"
#interface FBAppDelegate()
#property (strong,nonatomic) UINavigationController *navController;
#property (strong, nonatomic) FBViewController *mainController;
-(void) showLoginView;
#end
#implementation FBAppDelegate
#synthesize navController = _navController,
mainController =_mainController;
...
...
#end
and this is the *.h file:
#import <UIKit/UIKit.h>
#interface FBAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
UPDATE
added the import files
*SOLVED *
in FacebookSDK there is FBViewController.h already
The error you are getting is
Duplicate interface definition for class 'FBViewController'
And you are looking to class called FBAppDelegate. Try reading the error message again, it may help.
I am stuck with this problem. I have the following .h file:
#import <UIKit/UIKit.h>
#protocol MapSettingsViewDelegate
- (void)settingsDidUpdate:(BOOL)scheme;
#end
#interface MapSettingsViewController : UIViewController
#property (nonatomic, assign) id <MapSettingsViewDelegate> delegate;
#property (nonatomic, strong) IBOutlet UINavigationBar *navBar;
#property (nonatomic, strong) IBOutlet UINavigationItem *titleItem;
#property (nonatomic, strong) IBOutlet UITableView *TableView;
- (id)init;
- (IBAction)saveAction:(id)sender;
#end
When I declare the following:
#interface MapViewController : UIViewController <MapSettingsViewDelegate>
The compiler complains with the following message:
Cannot find protocol declaration for 'MapSettingsViewDelegate'
I have the same kind of delegate declaration in other files in the same project that are compiled without a glitch. I spent the last four hours trying to figuring out what I am doing wrong. Cleaning the project does not nothing.
Problem solved. As suggested in this answer, I created a new class with a different name and copied all coded from the previous class. Worked flawlessly. It seems XCode lost track of something.
You are running into preprocessor problems. Both of your classes import each other. I think you can solve this by adding #class below your protocol declaration as shown below; and moving your #import "mapViewController.h" line into MapSettingsViewController.m file. The #class tells the compiler that you will include the class content somewhere else (in the .m file).
Note that, on the top of your MapViewController.h class file you can include #import "MapSettingsViewController.h" just like normal. Hope this help. I ran into the same problem myself earlier.
#import <UIKit/UIKit.h>
#protocol MapSettingsViewDelegate
- (void)settingsDidUpdate:(BOOL)scheme;
#end
#class MapViewController
#interface MapSettingsViewController : UIViewController
#property (nonatomic, assign) id <MapSettingsViewDelegate> delegate;
...
...
Import the MapSettingsViewController.h file
Ok, it may be a dumb mistake. But I can't figure out what xcode wants from me.
So here is the header file
#import <Foundation/Foundation.h>
#import "TableViewController.h"
#end
#interface Settings : NSObject
- (id)init: (TableViewController*) TableControll;
#end
If there is no #end before interface it says expected identifier or (, and suggests adding #end there. If there is an #end it says end must appear in objective-c contex.
Ideas?
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>
#interface TableViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,MFMessageComposeViewControllerDelegate>
{
ControllerType controllerType;
}
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#property (retain, nonatomic) NSArray *dataArray;
#property (retain, nonatomic) NSArray *imageArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andType:(ControllerType)type;
#end
You have a #end before the #interface declaration in your Settings.h file.
It should read:
#import <Foundation/Foundation.h>
#import "TableViewController.h"
#interface Settings : NSObject
- (id)init: (TableViewController*) TableControll;
#end
Also, you haven't included the #import directive for ControllerType in your TableViewController.h which may be why you're getting obscure errors in your Settings.h file.
I have a protocol like this:
#import <Foundation/Foundation.h>
#protocol Prot1 <NSObject>
#required
- (void)methodInProtocol;
#end
This is a protocol for a delegate I want to store in a class like this:
#import <Cocoa/Cocoa.h>
#class Prot1;
#interface Class1 : NSObject
#property (nonatomic, strong) Prot1 *delegate;
- (void)methodInClass;
#end
The implementation for this class is like this:
#import "Class1.h"
#import "Prot1.h"
#implementation Class1
#synthesize delegate;
- (void)methodInClass {
[delegate methodInProt];
}
#end
When I build these pieces of code, I get the following error:
Receiver type 'Prot1' for instance message is a forward declaration
What is wrong here? I did understand that I have to do a forward declaration via #class for the protocol and I thought I only had to #import the protocol, in the class implementation... Isn't that right?
As it isnt a class, you have to define it as what it is - a protocol ;)
Use forward declaration: #protocol Prot1;;
And use the property like that:
#property (nonatomic, strong) id<Prot1> delegate;
So I have one class CommentViewController.h in which I have
#import "FirstViewController.h"
#protocol CommentViewControllerDelegate;
#interface CommentViewController : UIViewController {
id <CommentViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <CommentViewControllerDelegate> delegate;
- (IBAction)submit:(id)sender;
-(IBAction)cancel:(id)sender;
#end
#protocol CommentViewControllerDelegate
-(void)commentViewControllerDidFinish:(CommentViewController *)controller;
#end
I synthesized delegate in the implementation
I try to access the protocol in FirstViewController.h:
#import "CommentViewController.h"
#interface FirstViewController : UIViewController <CommentViewControllerDelegate>
And in the implantation of FirstViewController :
- (void)commentViewControllerDidFinish:(CommentViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
The error appears on this line:
#interface FirstViewController : UIViewController <CommentViewControllerDelegate>
Error: Cannot find protocol declaration for 'CommentViewControllerDelegate'; did you mean 'UISplitViewControllerDelegate'?
Am I missing something? I always have trouble with protocols and delegates.
You have a loop in your include files.
Remove this line from CommentViewController.h:
#import "FirstViewController.h"
It's not referenced in that header file, and if it were, you could simply put:
#class FirstViewController;
instead of including the whole file.