2016-05-05 175 views
2

我想弄清楚如何以編程方式將按鈕從一個視圖控制器切換到選項卡欄控制器中的第一個視圖控制器。以編程方式切換視圖控制器以顯示選項卡欄中的視圖控制器

目前我有一個視圖控制器與三個按鈕。當我按下按鈕時,我想再切換。這是我對這個屏幕的以下代碼。它被稱爲第二個視圖控制器。

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

+0

集tabbarcontroller根按鈕 –

+0

的自來水爲什麼要進行賽格瑞視圖控制器?你不想從標籤欄控制器的一個標籤切換到另一個標籤嗎? – Adeel

回答

2

我在Storyboard添加UITabBarController像下面請看圖片。

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

+0

self.performSegueWithIdentifier工作。非常感謝你 –

+0

歡迎:-) @AlexPriest –

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]; 
相關問題