繁体   English   中英

将数据传递回ViewController时在Delegate中出现问题

[英]Issue in Delegate while passing data back to ViewController

我在将数组数据从一个视图控制器(“ VC2”)传递回另一视图控制器(“ VC1”)时遇到问题。 我按规则做一切。 我在VC1中制定了正确的protocol

但不幸的是我无法取回数据。 这是我的代码:

VC2

protocol RecivedData {
    func dataRecived(nameArray: [String] , priceArray: [String])
}

var popUpdelegate : RecivedData?

@IBAction func nextBtnTapped(_ sender: UIButton) {
    print("Hello")
    let namedata = itemNameArr
    let namePrice = itemPriceArr

    self.popUpdelegate?.dataRecived(nameArray: namedata, priceArray: namePrice)
    print(namedata)
    print(namePrice)
    self.view.removeFromSuperview()
}

VC1

class HomeVC: UIViewController , RecivedData {
    func dataRecived(nameArray: [String], priceArray: [String]) {
        itemNameArr += nameArray
        itemPriceArr += priceArray
        print(itemNameArr, itemPriceArr)
        print ("This is HomeVC")
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "sendSegue"{
            let secondVC: AddOnItemPopUpVC = segue.destination as! AddOnItemPopUpVC
            secondVC.popUpdelegate = self
        }
     }
}

以此替换您的代码

protocol RecivedData : class {
    func dataRecived(nameArray: [String] , priceArray: [String])
}

weak var popUpdelegate : RecivedData?

现在它将开始工作。

确保segue名称中没有错字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM