首页 > 解决方案 > 我无法快速通过协议传递值

问题描述

我需要将值从ScheduleViewController确实选择行方法传递到MySchedularViewController表格单元格。

这是我的协议

protocol ScheduleViewControllerDelegate {
    func mySchedule(imgIcon: [String], proName: [String], proTime: [String])
}

从确实选择行方法中传递值

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        scheduleViewControllerDelegate?.mySchedule(imgIcon: ["hello"], proName: [programNameArray[indexPath.row]], proTime: [programTimeArray[indexPath.row]])

        print("Row Selected")
    }

在 myScheduleViewController 中获取协议

extension MySchedulerViewController: ScheduleViewControllerDelegate{
    func mySchedule(imgIcon: [String], proName: [String], proTime: [String]) {
        programImgUrlArray = imgIcon
        programNameArray = proName
        programTimeArray = proTime
        print("Hello ")
        DispatchQueue.main.async {
            self.scheduleTableView.reloadData()
        } 
    }
}

我为创建协议的 ViewController(ScheduleViewController) 创建了两个实例。它会导致任何问题吗?

标签: swiftuiviewcontrollerswift-protocols

解决方案


推荐阅读