簡體   English   中英

使用 UITabBarController 而不是 UINavigationController 時返回上一個 ViewController

[英]Return to previous ViewController when using UITabBarController but not UINavigationController

我使用我以編程方式創建的 UITabBarController 來呈現我在 Storyboard 中創建的 UIViewController。 我提出這些

let storyboard = UIStoryboard(name: "Main", bundle: nil)

// create tab one
let tabOne = storyboard.instantiateViewController(withIdentifier: "feedVC")
let tabOneBarItem = UITabBarItem(title: "feed", image: UIImage(named: "feed_icon"), selectedImage: UIImage(named: "feed_icon_selected"))
        tabOne.tabBarItem = tabOneBarItem

self.viewControllers = [tabOne]

在我沒有顯示 TabBar 的選項卡之一中,我在左上角有一個取消按鈕。 我更喜歡這樣將用戶發送回之前的 ViewController。 但是解決一個特定的問題。 下面的代碼有效,但 TabBarController 沒有顯示......實現取消/返回按鈕的最有效方法是什么。

我應該使用 UINavigationController - 還是有更好的選擇?

@IBAction func cancelBtnWasPressed(_ sender: Any) {
    let returnVC = storyboard?.instantiateViewController(withIdentifier: "feedVC")


    present(returnVC!, animated: true, completion: nil)


}

我找到了對我有用的解決方案。

斯威夫特 4:

self.tabBarController?.selectedIndex = 0

這將視圖移回 TabBarController 中的第一個選項卡

您必須dismiss當前的:

@IBAction func cancelBtnWasPressed(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
}

暫無
暫無

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

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