提问者:小点点

将数据从表单元格发送到视图控制器时,委托属性为nill


委派模式有一个问题,我试图将数据从一个表视图单元格发送到另一个视图控制器,但当我在第二个控制器(接收器)内设置delegate属性时,它是零,有人能给我解释一下吗,我对此是新手

协议和发送者类:

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)
    }
    
}

下面是receiver类:

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
    }
    
}

共1个答案

匿名用户

这里不需要委托您只需要传递模型项

nextController.sendedItem = arr[indexPath.row]

secondcontroller内部

var sendedItem:CellData?

内部的viewdidload设置值

编辑:

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