以编程方式切换视图控制器以在选项卡栏控制器中显示视图
Programmatically switch view controller to show views in Tab Bar 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) {
}
}
我在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
。
为 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];
我想弄清楚当我按下按钮时如何以编程方式将视图从一个视图控制器切换到选项卡栏控制器中的第一个视图控制器。
目前我有一个带有三个按钮的视图控制器。当我按下一个按钮时,我想切换。这是我为这个屏幕准备的以下代码。它被称为第二个视图控制器。
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) {
}
}
我在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
。
为 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];