开发者

Obj-C: Where to #import protocol header files

开发者 https://www.devze.com 2023-03-06 22:01 出处:网络
In an iOS app, I\'m defining my own protocol to use the delegate pattern among my custom view controllers.开发者_StackOverflow中文版 Which files should #import which other files? In other words, there

In an iOS app, I'm defining my own protocol to use the delegate pattern among my custom view controllers.开发者_StackOverflow中文版 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 protocol
  • MainViewController.m: Implements the protocol methods
  • SecondaryViewController.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 delegate

Which 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.


SecondaryViewController.m should #import 'MainViewController.h


SecondaryViewController.m should import the header as it uses the protocol methods.


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
}


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.

0

精彩评论

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

关注公众号