当我按下按钮时,我试图弄清楚如何以编程方式将视图从一个视图控制器切换到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) {
}
}
答案 0 :(得分:2)
我在UITabBarController
中添加了Storyboard
,如下所示,请参阅图片。
然后我写了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];