Home  >  Q&A  >  body text

objective-c - iOS中的delegate的原理是怎么回事?

如题,以前是个c#新手,在c#里也经常用委托,回来熟了lambda后都是直接写个lambda过去。当时没觉着二者区别多大。
但现在搞iOS有点晕了,怎么这里block和delegate是不同的东西?然后再看看委托的使用上怎么又觉着这不是和使用传统意义上的接口然后多态一个意思嘛?只是delegate有optional和request而已啊。
哎呦不对,我在写这个问题的时候突然感觉好像委托就是我所说的这个原理?求大神给解惑……

PHP中文网PHP中文网2735 days ago442

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-25 09:05:21

    The delegate in objc is not the same concept as in c#. The delegate in obj c is the delegation mode in the design pattern, which is generally a protocol (called interface in other languages, protocol in objc), optional and required (I guess You said request (probably misspelled), which means whether a certain method or variable when implementing the protocol is required or optional. For example, the table view itself does not know how many rows it wants to display or what it wants to display. When it needs to display it, it will ask its delegate (another weak reference of an object that obeys UITableViewDelegate/UITableViewDatasource). I want to display it. How many lines? How high is each row? Show something? If a class obeys UITableViewDelegate, some methods must be implemented (required) and some are optional (optional). Just think that you just use heavy keywords in objc and c#, but the meanings are different.

    reply
    0
  • Cancelreply