Obj-C:在哪里 #import 协议头文件
在 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 protocolMainViewController.m
: Implements the protocol methodsSecondaryViewController.h
: Declares a delegate instance variable and property of typeid <Protocol>
(with a forward declaration ofProtocol
)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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SecondaryViewController.m
应该#import 'MainViewController.h
SecondaryViewController.m
should#import 'MainViewController.h
secondaryViewController.m 应该导入标头,因为它使用协议方法。
SecondaryViewController.m should import the header as it uses the protocol methods.
假设 PrimaryViewController 有一些用于响应的委托方法。然后辅助视图控制器应该实现它的委托来使用它。委托方法在 PrmaryViewController 中声明并在其委托类中定义(此处为 secondaryViewController)
在主视图控制器中,您只需将委托声明为,
现在,在辅助视图控制器中只需导入主视图控制器,
在实现部分将委托分配给 self 作为,
并将主委托中描述的方法定义到辅助视图控制器中。
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,
Now, in the secondary view controller just import the primary view controller,
In the implementation section assign the delegate to self as,
and define the method described in the primarydelegate into secondary view controller.
这看起来倒退了。 MainViewController 定义一个协议然后实现它是没有意义的。协议的要点是您定义其他人要实现的方法。例如,查看 Xcode 4.2 Utility Application 项目模板:
结果是 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:
The result is that MainViewController can instantiate FlipsideViewController and set itself as FlipsideViewController's
delegate
. Now FlipsideViewController can talk back to MainViewController thru itsdelegate
property using FlipsideViewControllerDelegate methods while remaining agnostic about its real class.