Obj-C:在哪里 #import 协议头文件

发布于 2024-11-08 15:52:18 字数 519 浏览 0 评论 0原文

在 iOS 应用程序中,我定义自己的协议以在自定义视图控制器中使用委托模式。哪些文件应该#import 哪些其他文件?换句话说,我的案例涉及四个文件:

  • MainViewController.h:声明一个协议和一个视图控制器,而不是实现该协议
  • MainViewController.m:实现协议方法
  • SecondaryViewController.h:声明一个 id 类型的委托实例变量和属性(带有 Protocol 的前向声明)
  • SecondaryViewController.m:在委托上使用协议方法

哪些文件应该#import 哪些文件?我认为第二个视图控制器的标头中的前向声明就足够了,但是除非第二个标头或实现导入主标头,否则我会收到编译警告/错误。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

快乐很简单 2024-11-15 15:52:18

SecondaryViewController.m 应该 #import 'MainViewController.h

SecondaryViewController.m should #import 'MainViewController.h

凝望流年 2024-11-15 15:52:18

secondaryViewController.m 应该导入标头,因为它使用协议方法。

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

酒与心事 2024-11-15 15:52:18

假设 PrimaryViewController 有一些用于响应的委托方法。然后辅助视图控制器应该实现它的委托来使用它。委托方法在 PrmaryViewController 中声明并在其委托类中定义(此处为 secondaryViewController)
在主视图控制器中,您只需将委托声明为,

@protocol PrimaryDelegate
@interface PrimaryViewController : NSObject
   <id>PrimaryDelegate;
@end
@protocol PrimaryDelegate
   -(void)secondaryViewControllerWantsToCallThisDelegate;
@end

现在,在辅助视图控制器中只需导入主视图控制器,

 `#import "PrimaryViewController.h`
  @interface
  PrimaryViewController *primary;
  @end

在实现部分将委托分配给 self 作为,

 primary.delegate = self;

并将主委托中描述的方法定义到辅助视图控制器中。

-(void)secondaryViewControllerWantsToCallThisDelegate{
//some method definition here
}

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
}
混浊又暗下来 2024-11-15 15:52:18

这看起来倒退了。 MainViewController 定义一个协议然后实现它是没有意义的。协议的要点是您定义其他人要实现的方法。例如,查看 Xcode 4.2 Utility Application 项目模板:

// [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

结果是 MainViewController 可以实例化 FlipsideViewController 并将其自身设置为 FlipsideViewController 的 delegate。现在,FlipsideViewController 可以使用 FlipsideViewControllerDelegate 方法通过其 delegate 属性与 MainViewController 进行对话,同时对其真实类保持不可知。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文