以编程方式切换视图控制器以在标签栏控制器中显示视图

时间:2016-05-05 04:15:24

标签: ios swift uitabbarcontroller

当我按下按钮时,我试图弄清楚如何以编程方式将视图从一个视图控制器切换到Tab栏控制器中的第一个视图控制器。

目前我有一个带三个按钮的视图控制器。当我按下按钮时,我想切换。这是我为此屏幕提供的以下代码。它被称为第二个视图控制器。

    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

2 个答案:

答案 0 :(得分:2)

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

enter image description here enter image description here

然后我写了functions以下的帮助。

// 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

答案 1 :(得分:1)

为didFinishLaunchingWithOptions实施代码

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
firstViewController *firstTab = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:firstTab];

secViewController *secTab = [[firstViewController alloc] initWithNibName:@"secViewController" bundle:nil];
UINavigationController *navCntrl2 = [[UINavigationController alloc] initWithRootViewController:secTab];

thirdViewController *thirdTab = [[thirdViewController alloc] initWithNibName:@"thirdViewController" bundle:nil];
UINavigationController *navCntrl3 = [[UINavigationController alloc] initWithRootViewController:thirdTab];

UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = @[navCntrl1, navCntrl2, navCntrl3];

[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];