【问题标题】:Open ViewController on button在按钮上打开 ViewController
【发布时间】:2020-08-05 12:57:46
【问题描述】:

我有三个视图控制器,它们都有按钮切换到第四个视图控制器。在这第四个视图控制器上,我有一个后退按钮,我想将其带回到我最初所在的视图控制器。

我不能只使用控件拖动,因为它只能让您转到单个控制器。如何以编程方式将用户发送到最新的视图控制器?

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)
    }


}

【问题讨论】:

    标签: ios swift swift5


    【解决方案1】:

    尝试将控制器嵌入到 NavigationController 中,然后您可以使用:

    navigationController?.popViewController(animated: true)

    这将为您完成所需的功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2017-02-16
      • 2012-10-23
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多