本文介绍了在按钮上打开 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个视图控制器,它们都有按钮切换到第四个视图控制器.在第四个视图控制器上,我有一个后退按钮,我想通过它回到我原来所在的视图控制器.

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


}

推荐答案

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

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

navigationController?.popViewController(animated: true)

这将为您提供所需的功能.

This would do the desired functionality for you.

这篇关于在按钮上打开 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:08