问题
My scenario, I am having tabbar
with three viewcontroller
and first tabbar
viewcontroller I am showing tableview
with some data
. Whenever I am clicking the tableview
cell
I am passing the data to one special present model
popupcontroller. If I dismiss
popup controller I need to show directly to tabbar index 1
(I mean tabbar second viewcontroller
) also need to pass the values
.
Here, below code I tried
- After select the tableview cell passing values to popupview controller
- Popup close button click to pass the same value to tabbar index 1 (tabbar second viewcontroller)
Inside popup 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)
}
Once popup dismissed
showing tabbar
first viewcontroller
(index 0), so I added NotificationCenter
to call function and change the index.
回答1:
First of all you don't need to use NotificationCenter
for that because you can simply achieve it by passing the UITabBarController
object to next view controller when you select any cell in tableView
like shown below:
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)
}
and you need to declare var tabBar: UITabBarController?
into DetailViewController
which will hold the value passed by previous view controller.
Then when you dismiss the view controller you can simply select the another index of UITabBarController
with self.tabBar?.selectedIndex = 1
and you can simply pass the value to first index view controller like:
@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
})
}
and you need to declare objects in SecondViewController
like:
var selectedIndexFromFirstTab = 0
var tfData = ""
Which will hold the data of previous view.
Now your result will look like:
For more info you can refer THIS demo project.
EDIT:
So from your code I have seen that you have embed in navigation controllers to your tabBar
controllers so you need to update close_click
method like shown below:
@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
})
}
回答2:
You could use the completion handler of the dismiss method.
dismiss(animated: true, completion: nil)
Instead of passing nil, Pass a method to change the Tab bar to second tab.
The completion handler in the dismiss view controller, gets called when the dismissing of the view controller is completed.
viewController.dismissViewControllerAnimated(true, completion: {
FunctionToChangeToSecondTab()
})
来源:https://stackoverflow.com/questions/54730138/swift-passing-array-values-when-present-model-dismissing-time