我的场景,我有 tabbar
和三个 viewcontroller
和第一个 tabbar
viewcontroller 我正在显示 tableview
一些数据
。每当我单击 tableview
cell
时,我都会将数据传递给一个特殊的 present model
popupcontroller。如果我 dismiss
弹出 Controller 我需要直接显示到 tabbar index 1
(我的意思是 tabbar 第二个 viewcontroller
)也需要传递 值(value)观
。
在这里,下面是我试过的代码
- 选择 tableview 单元格后将值传递给 popupview Controller
- 弹出关闭按钮点击将相同的值传递给tabbar index 1(tabbar second viewcontroller)
内部弹出 View Controller
var item : Datum! //Datum Codable Root
@IBAction func close_click(_ sender: Any) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "disconnectPaxiSocket"), object: nil)
if let presenter = presentingViewController as? HomeViewController {
presenter.updatitem = item
}
dismiss(animated: true, completion: nil)
}
一旦弹出 dismissed
显示 tabbar
第一个 viewcontroller
(索引 0),所以我添加了 NotificationCenter
来调用函数并更改索引。
最佳答案
首先,您不需要为此使用 NotificationCenter
,因为您可以通过在选择任何单元格时将 UITabBarController
对象传递给下一个 View Controller 来简单地实现它在 tableView
中,如下所示:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
vc.selectedIndex = indexPath.row
vc.tabBar = self.tabBarController
self.present(vc, animated: true, completion: nil)
}
并且您需要将 var tabBar: UITabBarController?
声明到 DetailViewController
中,它将保存前一个 View Controller 传递的值。
然后,当您关闭 View Controller 时,您可以简单地使用 self.tabBar?.selectedIndex = 1
选择 UITabBarController
的另一个索引,您可以简单地将值传递给第一个索引 View Controller ,如:
@IBAction func close_click(_ sender: Any) {
dismiss(animated: true, completion: {
if let secondTab = self.tabBar?.viewControllers?[1] as? SecondViewController {
secondTab.selectedIndexFromFirstTab = self.selectedIndex
secondTab.tfData = self.userTF.text!
}
self.tabBar?.selectedIndex = 1
})
}
并且您需要在 SecondViewController
中声明对象,例如:
var selectedIndexFromFirstTab = 0
var tfData = ""
它将保存上一个 View 的数据。
现在您的结果将如下所示:
更多信息可以引用THIS演示项目。
编辑:
因此,从您的代码中,我看到您已将导航 Controller 嵌入到您的 tabBar
Controller 中,因此您需要更新 close_click
方法,如下所示:
@IBAction func close_click(_ sender: Any) {
dismiss(animated: true, completion: {
if let navView = self.tabBar?.viewControllers?[1] as? UINavigationController {
if let secondTab = navView.viewControllers[0] as? HomeViewController {
secondTab.selectedIndexFromFirstTab = self.selectedIndex
secondTab.item = self.item
secondTab.tfData = "Sample"
}
}
self.tabBar?.selectedIndex = 1
})
}
关于ios - 当前模型关闭时间时快速传递数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730138/