<legend id='sTCmA'><style id='sTCmA'><dir id='sTCmA'><q id='sTCmA'></q></dir></style></legend>

    <i id='sTCmA'><tr id='sTCmA'><dt id='sTCmA'><q id='sTCmA'><span id='sTCmA'><b id='sTCmA'><form id='sTCmA'><ins id='sTCmA'></ins><ul id='sTCmA'></ul><sub id='sTCmA'></sub></form><legend id='sTCmA'></legend><bdo id='sTCmA'><pre id='sTCmA'><center id='sTCmA'></center></pre></bdo></b><th id='sTCmA'></th></span></q></dt></tr></i><div id='sTCmA'><tfoot id='sTCmA'></tfoot><dl id='sTCmA'><fieldset id='sTCmA'></fieldset></dl></div>

    <small id='sTCmA'></small><noframes id='sTCmA'>

    1. <tfoot id='sTCmA'></tfoot>
        <bdo id='sTCmA'></bdo><ul id='sTCmA'></ul>

        协议和委托到底是什么,它们在 IOS 中是如何使用的?

        时间:2023-07-28
          <tbody id='5YxNn'></tbody>

        <small id='5YxNn'></small><noframes id='5YxNn'>

        <i id='5YxNn'><tr id='5YxNn'><dt id='5YxNn'><q id='5YxNn'><span id='5YxNn'><b id='5YxNn'><form id='5YxNn'><ins id='5YxNn'></ins><ul id='5YxNn'></ul><sub id='5YxNn'></sub></form><legend id='5YxNn'></legend><bdo id='5YxNn'><pre id='5YxNn'><center id='5YxNn'></center></pre></bdo></b><th id='5YxNn'></th></span></q></dt></tr></i><div id='5YxNn'><tfoot id='5YxNn'></tfoot><dl id='5YxNn'><fieldset id='5YxNn'></fieldset></dl></div>

              • <bdo id='5YxNn'></bdo><ul id='5YxNn'></ul>

                  <legend id='5YxNn'><style id='5YxNn'><dir id='5YxNn'><q id='5YxNn'></q></dir></style></legend><tfoot id='5YxNn'></tfoot>
                  本文介绍了协议和委托到底是什么,它们在 IOS 中是如何使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我真的对委托和协议的概念感到困惑.它们是否等同于 Java 中的接口和适配器类?它们是如何工作的?到目前为止,我读过的所有资源都没有帮助.委托是一种简单而强大的模式,其中程序中的一个对象代表另一个对象或与另一个对象协调.委托对象保持对另一个对象(委托)的引用,并在适当的时间发送消息给它."我不知道这是什么意思.有人可以解释一下它们是什么并举一个简单的例子吗?提前致谢!

                  I'm really confused about the concept of delegates and protocols. Are they equivalent of interfaces and adapter classes in Java? How do they work? None of the resources I've read were helpful so far. "Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it." I have no idea what this means. Can someone please explain what they are and give a simple example? Thanks in advance!

                  据我所知,

                  1) 委托实现协议(接口的另一个名称)

                  1) delegates implement protocols (another name for interfaces)

                  2) 对象注册一个委托(实现协议)

                  2) object registers a delegate (that implements a protocol)

                  3) 对象可以在委托上调用协议方法

                  3) object can call protocol methods on the delegate

                  因此,委托将对象与协议连接起来.

                  Therefore, a delegate is connecting the object with the protocol.

                  如果我错了,请纠正我.

                  Please correct me if I'm wrong.

                  我还是不明白为什么对象本身不能实现协议?本来可以轻松得多!

                  I still don't understand why the object itself can't implement a protocol? It could've been so much easier!

                  推荐答案

                  协议是一种指定一组方法的方法,如果它想与你的一个类一起工作,你希望一个类实现.委托和数据源,如 UITableViewDelegate 和 UITableViewDataSource 确实是协议.

                  Protocols are a way to specify a set of methods you want a class to implement if it wants to work with one of your classes. Delegates and Data Sources like UITableViewDelegate and UITableViewDataSource are protocols indeed.

                  您可以这样指定协议:

                  @protocol MyProtocol <NSObject>
                  
                  - (void)aRequiredMethod;
                  
                  @required
                  - (void)anotherRequiredMethod;
                  
                  @optional
                  - (void)anOptionalMethod;
                  
                  @end
                  

                  在@required 之后或任何其他说明符之前声明的方法是必需的,并且想要使用您的协议的类需要实现所有这些方法.您还可以通过在 @optional 说明符之后声明一些可选方法来声明它们.

                  Methods declared after the @required or before any other specifier are required and the classes that want to use your protocol need to implement all of them. You can also declare some optional methods by declaring them after the @optional specifier.

                  然后您可以在类的接口中指定一个类符合"协议(实现所需的方法):

                  You then can specify that a class "conforms" to a protocol (implements the required methods) in the interface of the class:

                  @interface MyClass <MyProtocol>
                  
                  @end
                  

                  您通常使用属性保留对符合协议的对象的引用.例如,要跟踪委托:

                  You usually keep a reference to an object conforming to a protocol using a property. For example, to keep track of a delegate:

                  @property (nonatomic, weak) id<MyProtocol> delegate;
                  

                  此时,在您的代码中,您只需要调用您想要调用的对象上的方法,该对象将保持引用并像使用任何其他方法一样实现您的协议:

                  At this point, in your code, you just have to call the method you want to call on the object that you're keeping reference of and that implements your protocol as you would with any other method:

                  [self.delegate aRequiredMethod];
                  

                  检查一个对象是否符合你可以调用的协议

                  To check whether an object conforms to a protocol you can call

                  [self.delegate conformsToProtocol:@protocol(MyProtocol)]
                  

                  检查一个对象是否实现了你可以调用的方法

                  To check whether an object implements a method you can call

                  [self.delegate respondsToSelector:@selector(anOptionalMethod)]
                  

                  有关更多信息,请查看 Apple 指南 使用协议.

                  For more information, check the Apple's guide Working With Protocols.

                  这篇关于协议和委托到底是什么,它们在 IOS 中是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以为代表提供“强"参考? 下一篇:使用 ARC 声明委托属性的推荐方法

                  相关文章

                  1. <legend id='dQv4Y'><style id='dQv4Y'><dir id='dQv4Y'><q id='dQv4Y'></q></dir></style></legend>
                  2. <i id='dQv4Y'><tr id='dQv4Y'><dt id='dQv4Y'><q id='dQv4Y'><span id='dQv4Y'><b id='dQv4Y'><form id='dQv4Y'><ins id='dQv4Y'></ins><ul id='dQv4Y'></ul><sub id='dQv4Y'></sub></form><legend id='dQv4Y'></legend><bdo id='dQv4Y'><pre id='dQv4Y'><center id='dQv4Y'></center></pre></bdo></b><th id='dQv4Y'></th></span></q></dt></tr></i><div id='dQv4Y'><tfoot id='dQv4Y'></tfoot><dl id='dQv4Y'><fieldset id='dQv4Y'></fieldset></dl></div>
                  3. <small id='dQv4Y'></small><noframes id='dQv4Y'>

                      <tfoot id='dQv4Y'></tfoot>
                        <bdo id='dQv4Y'></bdo><ul id='dQv4Y'></ul>