[英]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.我使用我以编程方式创建的 UITabBarController 来呈现我在 Storyboard 中创建的 UIViewController。 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.在我没有显示 TabBar 的选项卡之一中,我在左上角有一个取消按钮。 I prefer this to send the user back to the previous ViewController.
我更喜欢这样将用户发送回之前的 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.
下面的代码有效,但 TabBarController 没有显示......实现取消/返回按钮的最有效方法是什么。
Should I use UINavigationController - or are there better alternatives?我应该使用 UINavigationController - 还是有更好的选择?
@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:斯威夫特 4:
self.tabBarController?.selectedIndex = 0
This moves the view back to the first of the Tabs i the TabBarController这将视图移回 TabBarController 中的第一个选项卡
You have to dismiss
the current one :您必须
dismiss
当前的:
@IBAction func cancelBtnWasPressed(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.