Reputation: 33505
In an iOS app, I'm defining my own protocol to use the delegate pattern among my custom view controllers. Which files should #import
which other files? In other words, there are four files involved in my case:
MainViewController.h
: Declares a protocol and a view controller than implements the protocolMainViewController.m
: Implements the protocol methodsSecondaryViewController.h
: Declares a delegate instance variable and property of type id <Protocol>
(with a forward declaration of Protocol
)SecondaryViewController.m
: Uses the protocol method on the delegateWhich files should #import
which others? I'd think the forward declaration in the second view controller's header would be enough, but I get compile warnings/errors unless the second header or implementation imports the main header.
Upvotes: 2
Views: 4906
Reputation: 535831
This looks backwards. It doesn't make sense for MainViewController to define a protocol and then implement it. The point of a protocol is that you define methods for someone else to implement. For an example, look at the Xcode 4.2 Utility Application project template:
// [FlipsideViewController.h]
@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end
@interface FlipsideViewController : UIViewController
@property (weak, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate;
@end
// [MainViewController.h]
#import "FlipsideViewController.h" // so that it can see the protocol definition
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
@end
The result is that MainViewController can instantiate FlipsideViewController and set itself as FlipsideViewController's delegate
. Now FlipsideViewController can talk back to MainViewController thru its delegate
property using FlipsideViewControllerDelegate methods while remaining agnostic about its real class.
Upvotes: 0
Reputation: 21154
Let say PrimaryViewController that has some delegate methods for responding. Then the secondary view controller should implement its delegate to use it. The delegate methods are declared in PrmaryViewController and defined in its delegate class( here SecondaryViewController) In primary view controller you simply declare delegate as,
@protocol PrimaryDelegate
@interface PrimaryViewController : NSObject
<id>PrimaryDelegate;
@end
@protocol PrimaryDelegate
-(void)secondaryViewControllerWantsToCallThisDelegate;
@end
Now, in the secondary view controller just import the primary view controller,
`#import "PrimaryViewController.h`
@interface
PrimaryViewController *primary;
@end
In the implementation section assign the delegate to self as,
primary.delegate = self;
and define the method described in the primarydelegate into secondary view controller.
-(void)secondaryViewControllerWantsToCallThisDelegate{
//some method definition here
}
Upvotes: 2
Reputation: 1908
SecondaryViewController.m should import the header as it uses the protocol methods.
Upvotes: 2
Reputation: 44633
SecondaryViewController.m
should #import 'MainViewController.h
Upvotes: 3