Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

ios - Open ViewController on button

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

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

1 Reply

0 votes
by (71.8m points)

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

navigationController?.popViewController(animated: true)

This would do the desired functionality for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...