我有一个标签栏,我想根据用户的设置更改其项目。所以我需要能够以编程方式从标签栏添加/删除项目。这是我的代码:
class TabBarViewController: UITabBarController {
var controllerArray = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tabBar.tintColor = UIColor.greenColor()
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let controller1 = storyBoard.instantiateViewControllerWithIdentifier("Navigation2") as! nav2
let controller2 = storyBoard.instantiateViewControllerWithIdentifier("Explore") as! ExploreNavigation
let controller3 = storyBoard.instantiateViewControllerWithIdentifier("friends") as! ExploreNav
let controller4 = storyBoard.instantiateViewControllerWithIdentifier("controller3ID") as! controller3VC
controllerArray.append(controller1)
controllerArray.append(controller2)
controllerArray.append(controller3)
controllerArray.append(controller4)
self.tabBarController?.setViewControllers(controllerArray, animated: true)
}
}
但如果我注释掉其中一行,例如:
//controllerArray.append(controller2)
这样控制器不是controllersArray的一部分,控制器仍然显示在标签栏中。我做错了什么?
我也尝试了这个,它也没有用:
self.tabBarController?.viewControllers = controllerArray
答案 0 :(得分:4)
说完
self.setViewControllers(controllerArray, animated: true)
而不是
self.tabBarController?.setViewControllers(controllerArray, animated: true)
你是UITabBarController的子类,而不是UIViewController,所以你没有,或者至少不需要属性tabBarController。