我在视图控制器中实现自定义UITabBar
。我成功将UITabBar
委托设置为UIViewController
。
但是,在viewDidLoad上,我希望使用以下选项来选择UITabBar的第一项:
tabBar.selectedItem = tabBar.items[0] as UITabBarItem
但我一直遇到这个错误。
(UITabBar, didSelectItem: UITabBarItem) -> ()' does not have a member named 'selectedItem'
这是我的代码:
class ViewController: UIViewController, UITabBarDelegate {
@IBOutlet var tabBar : UITabBar!
@IBOutlet var placeHolderView : UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tabBar.selectedItem = tabBar.items[0] as UITabBarItem
switchView(0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
switch item.tag {
case 0:
switchView(0)
break
case 1:
switchView(1)
break
default:
break
}
}
...
}
更新:
让这个工作:
tabBar.selectedItem = tabBar.items![0] as? UITabBarItem
我仍然需要了解选项......
答案 0 :(得分:2)
我发现这个解决方案有效:
override func viewDidLoad() {
super.viewDidLoad()
for i in tabBar.items as [UITabBarItem]){
if i.tag == 0 {
tabBar.selectedItem = i
break;
}
}
switchView(0)
}
答案 1 :(得分:0)
我认为问题是UITabBar.items是可选的,所以你需要打开它来访问数组。
tabBar.selectedItem = tabBar.items![0] as UITabBarItem