ios - 以编程方式切换 View Controller 以在选项卡栏 Controller 中显示 View

标签 ios swift uitabbarcontroller

我试图弄清楚当我按下按钮时如何以编程方式将 View 从一个 View Controller 切换到选项卡栏 Controller 中的第一个 View Controller 。

目前我有一个带有三个按钮的 View Controller 。当我按下按钮时,我想切换。这是我针对该屏幕的以下代码。它被称为第二 View Controller 。

    import UIKit

    class SecondViewController: UIViewController {

    //Button Outlets
    @IBOutlet var ButtonEndOfShift: UIButton!
    @IBOutlet var ButtonMultiTimes: UIButton!
    @IBOutlet var ButtonEndAndLunch: UIButton!

    override func viewDidLoad() {

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //hides tab bar controller
        self.tabBarController?.tabBar.hidden = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //Changes screen for End Of Shift Button
    @IBAction func ChangeNextViewLow(sender: AnyObject) {


        self.performSegueWithIdentifier("segue", sender: nil)
    }

    //Changes screen for Lunch and End Of Shift Button
    @IBAction func ChangeNextViewMedium(sender: UIButton) {
    }

    //Changes screen for Multiple Times button
    @IBAction func ChangeNextViewHigh(sender: UIButton) {
    }

}

enter image description here

最佳答案

我在 Storyboard 中添加了 UITabBarController,如下所示,请查看图片。

enter image description here enter image description here

然后我编写了以下函数来帮助您。

// For navigate to Tabbar Controller
    @IBAction func btnClick () {
        self.performSegueWithIdentifier("GoToTabBar", sender: nil)
    }

    // For switching between tabs
    func switchTab (index : Int) {
       self.tabbarController.selectedIndex = index
    }

您还可以将 UITabBarController 设置为您的应用程序 RootViewController

关于ios - 以编程方式切换 View Controller 以在选项卡栏 Controller 中显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37042245/

相关文章:

swift - 向 UITabBarController 添加自定义按钮(在中心添加按钮)

ios - iOS 中的 UITabbarController 底部有空格,顶部有灰线

iOS 5 自动旋转不起作用

objective-c - 为什么我在 objective-c 中得到一个整数到指针的转换错误?

ios - popViewController Animated : from alertView:didDismissWithButtonIndex: 的断断续续的动画

ios - 截取屏幕截图,然后将其保存到相机胶卷。 swift 4.2,Xcode 10

ios - 在自定义 UICollectionView 类中实现时未正确调用数据源方法

ios - iPad 2 与 iPad Air 2 的数组排序字典。为什么相反?

ios - 如何在每个需要登录的 View Controller 上检查用户身份验证?

ios - 如何转换 NSUerdefaults AnyObject?到 SWIFT 阵列,以便我可以附加到它?