我的项目中有一个TabBarController
有两个视图。现在,我想在应用启动时将FirstViewController
设置为Initial View Controller
两个按钮。第一个按钮应显示FirstView
中的TabBarController
,第二个按钮应显示第二个按钮。当按下两个按钮中的一个按钮时,FirstViewController
应该会消失,并且只能使用TabBarViewController
中的标签在两个视图之间导航。
答案 0 :(得分:0)
我做了一些小编辑,并测试了我编写的代码并且它有效。控制从firstButton拖动到TabBarController并选择Kind作为“Show”。然后用secondButton做同样的事情。 在您使用两个按钮的视图中,我将其称为First:
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
}
}
}
答案 1 :(得分: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
}
}
不要忘记设置Storyboard ID
的{{1}}。