Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have three viewcontrollers that all have buttons switch to a fourth viewcontroller. On this fourth viewcontroller, I have a back button, which I want to take me back to the viewcontroller that I was originally at.

I can't just use the control drag since that only let's you go to a single controller. How can I programmatically have it send the user to the most recent 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)
    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
143 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try to embed the controllers inside NavigationController then you can use :

navigationController?.popViewController(animated: true)

This would do the desired functionality for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...