简体   繁体   中英

Return to previous ViewController when using UITabBarController but not UINavigationController

Im using a UITabBarController that I've created programatically to present UIViewControllers that I've created in Storyboard. I present these with

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]

in one of the tabs where I don't show the TabBar, I have a cancel button in the top left. I prefer this to send the user back to the previous ViewController. But settle with a specific one. The code below works, but the TabBarController do not show... what is the most efficient way to implement a cancel/return button.

Should I use UINavigationController - or are there better alternatives?

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


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


}

I found the solution that worked for me.

Swift 4:

self.tabBarController?.selectedIndex = 0

This moves the view back to the first of the Tabs i the TabBarController

You have to dismiss the current one :

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

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