簡體   English   中英

如何關閉選項卡欄控制器並單擊按鈕返回上一個視圖控制器

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

我正在建立這樣的流程

FirstViewController -> SecondViewController -> Tab Bar View Controller(由1.ThirdViewController和2.FourthVIewController組成)

我正在打開標簽欄視圖控制器作為 SecondViewController 的彈出窗口。 但是,當我單擊 ThirdViewController 中的按鈕運行 (self.dismiss(animated: true, completion: nil)) 時,它會返回到 FirstViewController。 我想回到 SecondViewController

添加代碼。 這就是我從 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)

這就是我嘗試關閉第三個視圖控制器的選項卡欄視圖控制器的方式

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

您在 secondViewController 中添加了 tabBarController 作為子視圖。 所以你需要從超級視圖中刪除那個 tabBarController 視圖。

為此,您需要一個 tabBarController 對象。

self.tabBarController?.view.removeFromSuperview()

您可以在流程中使用導航控制器,因此當您在 ThirdViewController 中時,請使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM