开发者

Properly declare delegation in Objective C (iPhone)

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"

@i开发者_如何学Cnterface 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?


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 <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 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 <NSObject>
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
@end

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

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


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


Here is another way that you could tackle this, similar to the solution proposed 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜