![](/img/trans.png)
[英]Does setting the selected item of a UITabBar calls tabBar:didSelectItem method?
[英]Having trouble manually setting UITabBar selected Item
我正在視圖控制器內實現自定義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
我仍然不得不考慮可選項...
我發現此解決方案正在工作:
override func viewDidLoad() {
super.viewDidLoad()
for i in tabBar.items as [UITabBarItem]){
if i.tag == 0 {
tabBar.selectedItem = i
break;
}
}
switchView(0)
}
我認為問題在於UITabBar.items是可選的,因此您需要將其拆開以訪問數組。
tabBar.selectedItem = tabBar.items![0] as UITabBarItem
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.