2016-10-22 130 views
0

我在我的項目中獲得了TabBarController兩個視圖。現在我想在應用啓動時設置一個FirstViewController,其中兩個按鈕爲Initial View Controller。第一個按鈕的TabBarController應顯示FirstView,第二個按鈕應顯示第二個按鈕。當兩個按鈕中的一個被按下時,FirstViewController應該消失,並且應該只能在兩個視圖之間導航,其中TabBarViewController中的選項卡。按鈕顯示選項卡

picture of the project

+1

您的意思是選項卡,而不是點擊。 – rmaddy

+0

您是否正在使用TabBarController來滿足上述要求? – Vinodh

+0

什麼是更好的解決方案? –

回答

0

我做了一些小修改,並測試我寫的代碼和它的作品。控制從firstButton拖到TabBarController並選擇Kind作爲「Show」。然後用secondButton做同樣的事情。 在與兩個按鈕你的看法,我首先把它叫做:

import Foundation 
import UIKit 

class First: UIViewController { 

    var firstWasClicked = false 
    @IBAction func firstButtonAction(sender: UIButton) { 
     firstWasClicked = true 
    } 

    @IBAction func secondButtonAction(sender: UIButton) { 
     firstWasClicked = false 
    } 


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     let controller = segue.destinationViewController as! TabBarController 
     controller.firstSelected = firstWasClicked 
    } 
} 

然後在TabBarController:

import Foundation 
import UIKit 

class TabBarController: UITabBarController { 

    var firstSelected = true 
    override func viewDidLoad() { 
     if(firstSelected) { 
      self.selectedIndex = 0 
     } 
     else { 
      self.selectedIndex = 1 
     } 
    } 

}

+0

此代碼實際上不起作用。 –

+0

它現在可以(測試) – DevB2F

+0

你可以請粘貼在你的項目中,你試過。 –

0

這可能是你想要的。

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBAction func didTapFirst(button: UIButton) { 
    showViewControllerAt(index: 0) 
} 

@IBAction func didTapSecond(button: UIButton) { 
    showViewControllerAt(index: 1) 
} 

func showViewControllerAt(index: NSInteger) { 
    let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController 
    tabBarController.selectedIndex = index 
    UIApplication.shared.keyWindow?.rootViewController = tabBarController 
} 
} 

不要忘記設置你的UITabBarControllerStoryboard ID