[英]Passing Data Between Controllers in Swift
我想在TableViewController和ViewController之間傳遞數據,程序不進入方法My swift代碼:
override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) {
let destView : ViewController = unwindSegue.destination as! ViewController
destView.min = Int(minTable)
destView.tableText = unitsText
}
我獲取數據:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tableCell = moneyArray[indexPath.row]
minTable = tableCell.val
unitsText = tableCell.name
let _ = navigationController?.popViewController(animated: true)
}
填寫我的表格代碼:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! TableViewCell
let tableShow = moneyArray[indexPath.row]
cell.nameCurrency?.text = tableShow.name
cell.valueCarrency?.text = "\(tableShow.val)"
return cell
}
如果要在用戶單擊主表視圖控制器中的單元格時打開詳細信息視圖控制器,則傳遞數據的正確方法是使用類似以下內容的方法:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "MyDetailView") {
// pass data to next view
if let viewController: MyDetailsViewController = segue.destinationViewController as? MyDetailsViewController {
viewController.units = mySelectedTableCell.unitsName
}
}
}
完整的文檔在這里 。
您在didSelectRow上使用popViewController,這意味着您將返回導航控制器,而不是推動展開序列或任何序列,因此不能使用prepareForSegue / unwind方法。
解決此問題的一種正確方法是使用委托。
您可以在此處找到有關此信息的更多信息: 從視圖控制器Xcode傳回數據
但是,如果要使用unwind segue,則必須在先前的viewController而不是當前的viewController上編寫unwind方法。 同樣,您將需要使用performSegue方法和您的解散segue的標識符。
您可以在此處查看有關放松警戒的更多信息: 放松警戒的用途是什么,如何使用它們?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.