swift - 未从 NSObject 类调用 Swift 委托函数(不是 2 ViewController 场景)
问题描述
抱歉,是的,这是另一个寻求 Swift 协议帮助的问题。过去在使用 prepare(ForSegue...) 标准的两个 VC 之间使用过这些都很好。但是,我的情况不包括 2 个 VC。
在我的应用程序中,我有一个视图控制器(称为 VC A),它有一个滑出设置菜单,其中包括一个更改组图像的选项。设置菜单的功能工作正常,除了显示在 VC A 上的 imageView 没有更新(除非用户离开 VC 并返回)。设置菜单选项完成时,VC A 上的委托功能未执行。
在此先感谢您的帮助。
我有一个 Protocols.swift 文件用于我正在使用的各种委托协议。这种情况的协议:
protocol UpdateGroupImageviewDelegate: class {
func updateGroupImageview()
}
滑出设置菜单相关代码:
class GroupSettingsLauncher: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var delegate: UpdateGroupImageviewDelegate?
// rest of the settings menu code, including one that executes the imagePicker
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// do the normal imagePicker stuff to get the selected image
// store the image, etc.
// call delegate method to update group image on chat screen
print("ABOUT TO CALL DELEGATE FOR UPATEGROUPIMAGEVIEW") // this prints correctly
self.delegate?.updateGroupImageview()
}
}
来自 viewController A 的代码:
class GroupChatViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UpdateGroupImageviewDelegate {
// Delegate Variable
var updateGroupImageviewDelegate: GroupSettingsLauncher?
override func viewDidLoad() {
super.viewDidLoad()
// Set this VC as the delegate for updating the group Imageview
updateGroupImageviewDelegate?.delegate = self
// rest of code
}
func updateGroupImageview() {
print("*** updateGroupImageview has been called!") // This is not printing
}
}
解决方案
推荐阅读
- ruby - $PATH 中的非 UTF8 字符会干扰 Homebrew Mac
- node.js - client.guilds.get 不是函数 DISCORD.JS
- flutter - 滚动视图内带有扩展小部件的颤振列
- azure - 无法在 Slack 中向机器人发送 DM。Bot 是使用 azure botframework 创建的,并使用了渠道集成
- pyinstaller - 微软后卫停止 pyinstaller
- android - Flutter 创建父目录失败
- arrays - 打印数组但突出显示特定值 {emu8086}
- microsoft-graph-api - 使用 curl 的 msgraph 客户端断言证书,如何生成断言
- css - Safari 13 中不支持网格模板列
- bokeh - 散景/面板不会与 Jupyter Lab 内联渲染