返回上一个屏幕时如何在控制器之间传输数据? Swift
How I can transfer data between controllers when returning to the last screen? Swift
在应用程序中,我被要求 select 一个类别,当我点击时,我从 tableView 移动到下一个屏幕,当我点击所需的类别时,它应该被保存并且 return 到以前的控制器,而不是 "select a category" 我选择的类别将是。
我使用这种方法navigationController?.popViewController(animated: true)
,这让我回到了上一个屏幕。
据我了解,未调用 prepare,但由于我确切地知道我要去哪里,所以我可以在我调用 pop ... 的方法中访问我要切换到的控制器并向其传递必要的信息特性?但是怎么办?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentCell = categories[indexPath.row]
/?
/?
navigationController?.popViewController(animated: true)
}
当您转到 select 屏幕时,请执行
let vc = ///
vc.delegate = self
// push
然后里面 didASelectRowAt
class CategoryViewController:UIViewController {
weak var delegate:PreviousVC?
////
}
delegate?.sendData(someData)
navigationController?.popViewController(animated: true)
didASelectRowAt
到
里面的另一个选项
// access previous vc
let previousVC = self.navigationController!.viewControllers[self.navigationController!.viewControllers.count - 2] as! PreviousVc
previousVC.sendData(someData)
// pop here
编辑: 在 firstVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondVC = segue.destination as? SecondViewController {
secondVC.delegate = self
}
}
您可以使用闭包将所选类别传递给上一个视图控制器
class FirstViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondVC = segue.destination as? SecondViewController {
secondVC.selectedOption = { selectedOption in
print(selectedOption)
//change category title here
}
}
}
}
class SecondViewController: UIViewController {
var selectedOption: ((String) -> Void)?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedOption?("Selected Option String")
navigationController?.popViewController(animated: true)
}
}
也许你应该在移动到类别选择控制器时传递 "Completion handler",例如
(String)->(Void)
当你弹出它时,在这个 clouser 中传递你的类别
在应用程序中,我被要求 select 一个类别,当我点击时,我从 tableView 移动到下一个屏幕,当我点击所需的类别时,它应该被保存并且 return 到以前的控制器,而不是 "select a category" 我选择的类别将是。
我使用这种方法navigationController?.popViewController(animated: true)
,这让我回到了上一个屏幕。
据我了解,未调用 prepare,但由于我确切地知道我要去哪里,所以我可以在我调用 pop ... 的方法中访问我要切换到的控制器并向其传递必要的信息特性?但是怎么办?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentCell = categories[indexPath.row]
/?
/?
navigationController?.popViewController(animated: true)
}
当您转到 select 屏幕时,请执行
let vc = ///
vc.delegate = self
// push
然后里面 didASelectRowAt
class CategoryViewController:UIViewController {
weak var delegate:PreviousVC?
////
}
delegate?.sendData(someData)
navigationController?.popViewController(animated: true)
didASelectRowAt
到
// access previous vc
let previousVC = self.navigationController!.viewControllers[self.navigationController!.viewControllers.count - 2] as! PreviousVc
previousVC.sendData(someData)
// pop here
编辑: 在 firstVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondVC = segue.destination as? SecondViewController {
secondVC.delegate = self
}
}
您可以使用闭包将所选类别传递给上一个视图控制器
class FirstViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let secondVC = segue.destination as? SecondViewController {
secondVC.selectedOption = { selectedOption in
print(selectedOption)
//change category title here
}
}
}
}
class SecondViewController: UIViewController {
var selectedOption: ((String) -> Void)?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedOption?("Selected Option String")
navigationController?.popViewController(animated: true)
}
}
也许你应该在移动到类别选择控制器时传递 "Completion handler",例如
(String)->(Void)
当你弹出它时,在这个 clouser 中传递你的类别