更改嵌入式 TabBarController 中的选项卡
Change the tabs in a Embedded TabBarController
我有一个视图控制器,它有两个按钮和一个嵌入式 TabBarController。
我想使用两个按钮而不是标签栏项目。并且由于某种原因无法使其工作。
主视图
import UIKit
class ViewController: UIViewController {
var currentTab = 0
@IBOutlet weak var container: UIView!
@IBAction func item1Button(_ sender: UIButton) {
currentTab = 0
}
@IBAction func item2Button(_ sender: UIButton) {
currentTab = 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toControllers"{
if let vc = segue.destination as? TabsViewController {
vc.selectedIndex = currentTab
}
}
}
}
TabBarController
import UIKit
class TabsViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
故事板中有两个项目,所以项目 0 和 1 应该没问题。
可能很简单,自己想不通
谢谢!
我弄明白了,这是解决方案:
将主控制器更改为:
class ViewController: UIViewController {
var currentTab = 1
private var tabViewController: UITabBarController?
@IBOutlet weak var container: UIView!
@IBAction func item1Button(_ sender: UIButton) {
print("click item1")
tabViewController?.selectedIndex = 0
}
@IBAction func item2Button(_ sender: UIButton) {
print("click item2")
tabViewController?.selectedIndex = 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toControllers"{
if let vc = segue.destination as? TabsViewController {
tabViewController = vc
}
}
}
我有一个视图控制器,它有两个按钮和一个嵌入式 TabBarController。 我想使用两个按钮而不是标签栏项目。并且由于某种原因无法使其工作。
主视图
import UIKit
class ViewController: UIViewController {
var currentTab = 0
@IBOutlet weak var container: UIView!
@IBAction func item1Button(_ sender: UIButton) {
currentTab = 0
}
@IBAction func item2Button(_ sender: UIButton) {
currentTab = 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toControllers"{
if let vc = segue.destination as? TabsViewController {
vc.selectedIndex = currentTab
}
}
}
}
TabBarController
import UIKit
class TabsViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
故事板中有两个项目,所以项目 0 和 1 应该没问题。
可能很简单,自己想不通
谢谢!
我弄明白了,这是解决方案:
将主控制器更改为:
class ViewController: UIViewController {
var currentTab = 1
private var tabViewController: UITabBarController?
@IBOutlet weak var container: UIView!
@IBAction func item1Button(_ sender: UIButton) {
print("click item1")
tabViewController?.selectedIndex = 0
}
@IBAction func item2Button(_ sender: UIButton) {
print("click item2")
tabViewController?.selectedIndex = 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toControllers"{
if let vc = segue.destination as? TabsViewController {
tabViewController = vc
}
}
}