如何调用多个参数

时间:2018-12-08 15:54:28

标签: ios swift

我尝试在以下代码中调用两个参数:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let valueToPass = jobs[indexPath.row].text
    let passUserName = jobs[indexPath.row].addedByUser
    performSegue(withIdentifier: "toDetails", sender: valueToPass, passUserName)
}

但是我收到错误消息“调用中的额外参数” 我该怎么解决?

编辑:我尝试了以下操作:

    let passItems = (valueToPass, passUserName)
     performSegue(withIdentifier: "toDetails", sender: [passItems])

但是在以下VC上,标签为空...

2 个答案:

答案 0 :(得分:1)

使用以下参数声明performSegue方法:

withIdentifier identifier: String, sender: Any?

...因此您无法添加其他参数。

我想您有自定义数据模型

struct Job {
    var text: String
    var addedByUser: Bool
}

因此,如果您将整个job作为发件人传递,那么对您来说是最好的。

performSegue(withIdentifier: "toDetails", sender: jobs[indexPath.row])

然后在准备segue方法时,您可以将发件人转换为Job,并设置目标控制器的工作属性

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toDetails" {
        let destinationVC = segue.destionation as! JobDetailViewController
        destinationVC.job = sender as! Job
    }
}

所以请不要忘记将text的{​​{1}}和addedByUser属性替换为JobDetailViewController

job

答案 1 :(得分:0)

尝试一下:

performSegue(withIdentifier: "toDetails", sender: indexPath)

然后,重写此方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {     
    if segue.identifier == "toDetails" {
        if let indexPath = sender as? IndexPath {
            if let nextVC = segue.destination as? DetailsViewController {
                let valueToPass = jobs[indexPath.row].text
                let passUserName = jobs[indexPath.row].addedByUser
                nextVC.text = valueToPass
                nextVC.userName = passUserName
            }
        }
    }
}

请注意,您应该在DestinationViewController中具有自定义属性.text和.userName。