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

时间:2018-07-20 13:42:59

标签: ios swift

我在将数组数据从一个视图控制器(“ 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
        }
     }
}

1 个答案:

答案 0 :(得分:-1)

以此替换您的代码

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

还有

weak var popUpdelegate : RecivedData?

现在它将开始工作。

确保segue名称中没有错字。