首页 > 解决方案 > 未从 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

    }

}

标签: swiftdelegates

解决方案


推荐阅读