views:

260

answers:

2

Ok, This has been explained a few times (I got most of the way there using this post on SO), but I am missing something. I am able to compile cleanly, and able to set the delegate as well as call methods from the delegate, but I'm getting a warning on build:

No definition of protocol 'DetailViewControllerDelegate' is found

I have a DetailViewController and a RootViewController only. I am calling a method in RootViewController from DetailViewController. I have the delegate set up as so:

In RootViewController.h:

#import "DetailViewController.h"

@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate, DetailViewControllerDelegate> //Error shows up here
{
//Some Stuff Here
}
//Some other stuff here
@end

In RootViewController.m I define the delegate when I create the view using detailViewController.delegate = self

In DetailViewController.h:

@protocol DetailViewControllerDelegate;

#import "RootViewController.h"

@interface DetailViewController : UITableViewController <UITextFieldDelegate>
{
    id <DetailViewControllerDelegate> delegate;
}

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

@end

@protocol DetailViewControllerDelegate

//some methods that reside in RootViewController.m

@end

I feel weird about declaring the protocol above the import in DetailViewController.h, but if I don't it doesn't build. Like I said, the methods are called fine, and there are no other errors going on. What am I missing here?

A: 

Try:

In DetailViewController.h:

#import "RootViewController.h"    

@protocol DetailViewControllerDelegate <NSObject>

//some methods that reside in RootViewController.m

@end

@interface DetailViewController : UITableViewController <UITextFieldDelegate>
{
  id <DetailViewControllerDelegate> delegate;
}

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

@end
pheelicks
When I do that, I end up with errors in the method on build. So I get `Expected ')' before DetailViewController`. The method doesn't have any typos in it.-EDIT TO ADD-It does get rid of the warning though.
Gordon Fontenot
Try moving the import to before the protocol (see updated answer)
pheelicks
+1  A: 

Hi,

pheelicks is pretty much there but it looks like some of your protocol methods also use the DetailViewController class, I imagine it looks something like this :

@protocol DetailViewControllerDelegate   
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
@end

@class DetailViewController : UITableViewController <UITextFieldDelegate> {
    id <DetailViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
@end

and you haven't defined DetailViewController yet so you will get an error in the protocol definition.

You can fix this in two ways :

a) Declare (but don't define yet) the class before the protocol

@class DetailViewController;

@protocol DetailViewControllerDelegate
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
@end

b) Just use UITableViewController instead of DetailViewController in your protocol methods.

@protocol DetailViewControllerDelegate
- (void) controller:(UITableViewController *)controller hasSomething:(id)thing;
@end

Personally, I choose solution (a) but it really depends on what you're trying to do.

Hope that helps.

deanWombourne
I chose solution (a). It works great. Thanks a lot.
Gordon Fontenot