简体   繁体   中英

How to dismiss a Tab Bar Controller and go back to previous view controller on click of button

I am building a flow like this

FirstViewController -> SecondViewController - > Tab Bar View Controller (consists of 1. ThirdViewController and 2. FourthVIewController)

I am opening Tab Bar View Controller as a pop up form the SecondViewController. However, when I run (self.dismiss(animated: true, completion: nil)) on click of a button in ThirdViewController, it goes back to the FirstViewController. I want to go back to the SecondViewController

Adding code. This is how I open the tab bar view controller from my SecondViewController

let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tabBarVC") as! UITabBarController
    self.addChildViewController(popupVC)
    popupVC.view.frame = self.view.frame
    self.view.addSubview(popupVC.view)
    popupVC.didMove(toParentViewController: self)

And this is how I try to close the tab bar view controller form Third View Controller

  self.dismiss(animated: true, completion: nil))

You added tabBarController in secondViewController as subView. So you need to remove that tabBarController view from super view.

For that, you need a tabBarController object.

self.tabBarController?.view.removeFromSuperview()

You can use a navigation controller in your flow, so when you are in ThirdViewController use this:

if let vc = self.storyboard?.instantiateViewController(withIdentifier: "second") as? SecondViewController {
    self.navigationController?.popToViewController(vc, animated: true)
}

下面的代码能够删除 tabview 并带我回到 SecondViewController

self.tabBarController?.view.removeFromSuperview()

切换到 UITabBar 上的其他项,其中 0 插入 UTTabBar 项的索引:

self.tabBarController?.selectedIndex = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM