ios - 在按钮上打开 ViewController
问题描述
我有三个视图控制器,它们都有按钮切换到第四个视图控制器。在这第四个视图控制器上,我有一个后退按钮,我想将它带回到我最初所在的视图控制器。
我不能只使用控制拖动,因为那只能让您转到单个控制器。如何以编程方式将用户发送到最新的视图控制器?
import UIKit
class ViewController: UIViewController {
let tabBar = UITabBarController()
var selectedIndex: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tab()
}
@IBAction func goto(_ sender: Any) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "FrekansViewController") as! FrekansViewController
self.present(nextViewController, animated:true, completion:nil)
}
func tab() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC = UIViewController()
var streamVC = UIViewController()
var liveVC = UIViewController()
var searchVC = UIViewController()
streamVC = storyboard.instantiateViewController(withIdentifier: "StreamViewController")
liveVC = storyboard.instantiateViewController(withIdentifier: "LiveViewController")
searchVC = storyboard.instantiateViewController(withIdentifier: "SearchViewController")
tabBar.viewControllers = [homeVC,streamVC, liveVC, searchVC]
let itemHome = UITabBarItem(title: "Home", image: UIImage.init(systemName: "house.fill") , tag:0)
let itemStream = UITabBarItem(title: "List", image: UIImage.init(systemName: "waveform.path") , tag:1)
let itemLive = UITabBarItem(title: "Radio", image: UIImage.init(systemName: "play.fill") , tag:2)
let itemSearch = UITabBarItem(title: "Search", image: UIImage.init(systemName: "magnifyingglass"), tag: 3)
homeVC.tabBarItem = itemHome
streamVC.tabBarItem = itemStream
liveVC.tabBarItem = itemLive
searchVC.tabBarItem = itemSearch
self.view.addSubview(tabBar.view)
}
}
解决方案
尝试将控制器嵌入 NavigationController 中,然后您可以使用:
navigationController?.popViewController(animated: true)
这将为您完成所需的功能。
推荐阅读
- google-app-engine - Safari 'origin is not allowed' 阻止 AppEngine 身份感知代理 https://accounts.google.com/o/oauth2/v2/auth
- amazon-web-services - Amazon RDS 跨区域双向数据复制
- netlogo - 修补网络以“连接点”并评估属于代理“家庭范围”的整个区域
- python - Python:列表理解,将 2 个列表组合成 1 个具有唯一值的列表
- django - Django Performance 为什么总时间和SQL时间之间有巨大的时间差距
- sql-server - 当在 SQL 中对一行执行 SUM() 时,如何添加一个标志来说明它是否实际求和?
- javascript - 使用 nodeJS 拉取 SOAP XML 数据
- android - 从这里开始使用 STARTER 计划的卡车转弯导航问题
- laravel - 使用另一个字段验证一个字段
- python - 当我尝试使用 nonlinsolve 求解非线性方程组时出错