委托 属性 在将数据从 table 单元格发送到视图控制器时为空

delegate property is nill while sending data from table cell to a view controller

我的委托模式有问题,我正尝试将数据从 table 视图单元发送到另一个视图控制器,但是当我在第二个视图控制器中设置委托 属性控制器(接收器),它是零,谁能给我解释一下,我是新手

协议和发送者class:

import UIKit

protocol SendDataDelegate {
    func updateProperties(_ data: CellData)
}

class ViewController: UIViewController {
        
    @IBOutlet weak var tableView: UITableView!
        
    var arr: [CellData] = [
        CellData(img: UIImage(named: "1")!, str1: "welcome1", str2: "hello1"),
        CellData(img: UIImage(named: "2")!, str1: "welcome2", str2: "hello2"),
        CellData(img: UIImage(named: "3")!, str1: "welcome3", str2: "hello3"),
        CellData(img: UIImage(named: "4")!, str1: "welcome4", str2: "hello4"),
        CellData(img: UIImage(named: "5")!, str1: "welcome5", str2: "hello5"),
        CellData(img: UIImage(named: "6")!, str1: "welcome6", str2: "hello6"),
        CellData(img: UIImage(named: "7")!, str1: "welcome7", str2: "hello7"),
        CellData(img: UIImage(named: "8")!, str1: "welcome8", str2: "hello8"),
        CellData(img: UIImage(named: "9")!, str1: "welcome9", str2: "hello9"),
        CellData(img: UIImage(named: "10")!, str1: "welcome10", str2: "hello10")
    ]
    
    var delegate: SendDataDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
    }
        
    fileprivate func setupTableView() {
        tableView.dataSource = self
        tableView.delegate   = self
        tableView.register(
            UINib(nibName: Constants.cellNibName, bundle: nil),
            forCellReuseIdentifier: Constants.cellIdentifier
        )
    }
}

extension ViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(
            withIdentifier: Constants.cellIdentifier,
            for: indexPath
        ) as! TableCell
        cell.picture.image = arr[indexPath.row].img
        cell.label1.text   = arr[indexPath.row].str1
        cell.label2.text   = arr[indexPath.row].str2
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(300.0)
    }
}

extension ViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let storyboard = UIStoryboard(name: Constants.storyboardName, bundle: nil)
        let nextController = storyboard.instantiateViewController(
            withIdentifier: Constants.destinationController
        ) as! SecondController
        
        var data = arr[indexPath.row]
        delegate?.updateProperties(data)
        present(nextController, animated: true, completion: nil)
    }
    
}

这是接收器 class:

import UIKit

class SecondController: UIViewController {
        
    @IBOutlet weak var picture: UIImageView! {
        didSet {
            picture.image = img
        }
    }
    @IBOutlet weak var label11: UILabel! {
        didSet {
            label11.text = s1
        }
    }
    @IBOutlet weak var label22: UILabel! {
        didSet {
            label22.text = s2
        }
    }

    var s1 = ""
    var s2 = ""
    var img = UIImage()
        
    override func viewDidLoad() {
        super.viewDidLoad()
    
        view.backgroundColor = .systemRed
        
        let controller = UIStoryboard(name: Constants.storyboardName, bundle: nil).instantiateViewController(
        withIdentifier: Constants.firstController
        ) as! ViewController
        controller.delegate = self
        print(s1)
    }
    
}

extension SecondController: SendDataDelegate {
    
    func updateProperties(_ data: CellData) {
        self.s1 = data.str1
        self.s2 = data.str2
        self.img = data.img
    }
    
}

这里不需要delegate只需要传model item

nextController.sendedItem = arr[indexPath.row]

里面SecondController

var sendedItem:CellData?

里面viewDidLoad设置值

编辑:

nextController.updateProperties(data)
present(nextController, animated: true, completion: nil)

好的,我想通了,在我的发件人内部 class 在这个函数中:

extension ViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let storyboard = UIStoryboard(name: Constants.storyboardName, bundle: nil)
        let nextController = storyboard.instantiateViewController(
            withIdentifier: Constants.destinationController
        ) as! SecondController
        
        var data = arr[indexPath.row]
        delegate?.updateProperties(data)
        present(nextController, animated: true, completion: nil)
    }
    
}

我在将数据传递给 updateProperties() 函数之前添加了这一行 delegate = nextController 并且有效