无法将数据传递到viewController

时间:2018-08-06 16:26:09

标签: swift navigationcontroller pass-data

我有两个数组,将区域名称和网站地址存储为字符串。 另外,我有9个区域按钮。当您按下其中一个按钮时,根据您按下的区域按钮,它将显示不同的图片。 这是我的代码:

@IBAction func westfjordsBtn(_ sender: UIButton) {

    vcContainer[0].address=imgAddress[0]
    vcContainer[0].areaName=areaName[0]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[0], animated: true)
}
@IBAction func northBtn(_ sender: UIButton) {
    vcContainer[1].address=imgAddress[1]
    vcContainer[1].areaName=areaName[1]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[1], animated: true)
}
@IBAction func northeastBtn(_ sender: UIButton) {

        vcContainer[2].address=imgAddress[2]
    vcContainer[2].areaName=areaName[2]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[2], animated: true)
}
@IBAction func eastBtn(_ sender: UIButton) {

    vcContainer[3].address=imgAddress[3]
    vcContainer[3].areaName=areaName[3]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[3], animated: true)
}
@IBAction func southeastBtn(_ sender: UIButton) {

        vcContainer[4].address=imgAddress[4]
    vcContainer[4].areaName=areaName[4]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[4], animated: true)
}
@IBAction func entireBtn(_ sender: UIButton) {

        vcContainer[5].address=imgAddress[5]
    vcContainer[5].areaName=areaName[5]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[5], animated: true)
}
@IBAction func southBtn(_ sender: UIButton) {

        vcContainer[6].address=imgAddress[6]
    vcContainer[6].areaName=areaName[6]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[6], animated: true)
}
@IBAction func westBtn(_ sender: UIButton) {

        vcContainer[7].address=imgAddress[7]
    vcContainer[7].areaName=areaName[7]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[7], animated: true)
}
@IBAction func westsouthBtn(_ sender: UIButton) {

        vcContainer[8].address=imgAddress[8]
    vcContainer[8].areaName=areaName[8]
    loadViewIfNeeded()
    self.navigationController?.pushViewController(self.vcContainer[8], animated: true)
}
override func viewDidLoad() {
    super.viewDidLoad()
    for i in 0 ..< vcContainer.count {
         vcContainer[i] = self.storyboard?.instantiateViewController(withIdentifier: "AreaRoadDetailViewController") as! AreaRoadDetailViewController
    }

}

但是,代码是从第一个按钮函数复制而来的,只需要更改数组索引即可。只有第一个按钮可以正常工作,当我按下其他按钮时,它始终无法将imgAddress传递给AreaRoadDetailViewController。

在AreaRoadDetailViewController中,它始终显示地址为nil,但是区域名称可以正确显示。为什么这么狂野?只有一个按钮可以工作!我该如何解决?谢谢

1 个答案:

答案 0 :(得分:0)

也许您已经选择了所有按钮并拖动到class,这样只会设置第一个按钮。您必须分别为每个按钮设置一个方法。在检查器中检查按钮的链接。同时删除不必要的链接和方法。