Declare delegation correctly in Objective C (iPhone)

Ok, this has been explained a few times (I got most of this way using this post on SO ), but I'm missing something. I can compile and configure the delegate as well as the calling methods from the delegate, but I get a build warning:

No definition of protocol 'DetailViewControllerDelegate' is found

      

I only have DetailViewController and RootViewController. I am calling a method on RootViewController from DetailViewController. I have a delegate configured like this:

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 am defining a delegate when I create a view withdetailViewController.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

      

It seems strange to me to declare the protocol above the import in DetailViewController.h

, but if I don't create it, it won't be created. As I said, the methods are called penalties and no other errors happen. What am I missing here?

+2


a source to share


3 answers


pheelicks pretty much is, but it looks like some of your protocol methods also use a class DetailViewController

, I think it looks something like this:

@protocol DetailViewControllerDelegate <NSObject>
- (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

it yet, however, you will get an error in the protocol definition.

You can fix this in two ways:

a) Declare (but not yet define) the class before the protocol



@class DetailViewController;

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

      

b) Just use the protocol UITableViewController

instead DetailViewController

in your methods.

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

      

Personally, I choose the solution (s), but it really depends on what you are trying to do.

Hope it helps.

+3


a source


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

      

0


a source


Here's another way you could handle this, similar to the solution suggested by deanWombourne.

@protocol DetailViewControllerDelegate;

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

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

@end

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

      

0


a source







All Articles