我不明白代表们的意思 [英] I cannot understand what delegates are about

查看:55
本文介绍了我不明白代表们的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法快速理解代表的用途

I cannot understand what Delegates are used for in swift

我试图在任何可能的站点中搜索代表的含义及其用途,但我无法完全理解他们的含义.

I tried to search in any possible site what a delegate is and for what it is used for but I cannot completely grasp the meaning of them.

f.e.在此代码中

extension SignUpViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    }
}

分配给ViewController的委托应该做什么?以及我该如何使用它们?

what are the delegates assigned to the ViewController supposed to do? and how can I understand how to use them?

推荐答案

我想比喻思考:

将有一个"发言人"和一个"听众".

There will be a "speaker" and a "listener".

UIImagePickerController 已经配置为在事件发生时"",因此,当发生类似 didFinishPickingMediaWithInfo 之类的事件时,它会"".

UIImagePickerController is already configured to "speak" when events happen, so when something like didFinishPickingMediaWithInfo happens, it "speaks" .

在您的方案中, UIViewController 应该是"监听器",此配置为:

In your scenario, the UIViewController should be the "listener", this is configured with:

picker.delegate = self ,假设 UIViewController 实现了 UIImagePickerControllerDelegate .

换句话说,当 picker "说话"时,将 UIViewController 设置为"监听器".

In other words, make the UIViewController the "listener" for when the picker "speak".

通过 UIImagePickerControllerDelegate ,选择器"说话"的所有内容都会被"收听",无论谁是"收听".

And everything that the picker "speak", will be "listened" via UIImagePickerControllerDelegate by whoever is "listening".

注意:这是非常肤浅和抽象的,当然不止于此,但是我正在考虑OP的情况.

Note: This is very superficial and abstract, of course it goes much beyond this, but I'm considering the OP situation.

这篇关于我不明白代表们的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆