【问题标题】:Programmatically switch view controller to show views in Tab Bar Controller以编程方式切换视图控制器以在选项卡栏控制器中显示视图
【发布时间】:2016-05-05 04:15:24
【问题描述】:

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

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

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

}

【问题讨论】:

  • 点击按钮将 tabbarcontroller 设置为根视图控制器
  • 你为什么要进行segue?您不想从标签栏控制器的一个标签切换到另一个标签吗?

标签: ios swift uitabbarcontroller


【解决方案1】:

我在Storyboard 中添加了UITabBarController,如下图所示。

然后我写了以下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

【讨论】:

  • self.performSegueWithIdentifier 工作。非常感谢
  • 欢迎 :-) @AlexPriest
【解决方案2】:

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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多