gpt4 book ai didi

ios - 手动设置 UITabBar selected Item 时遇到问题

转载 作者:行者123 更新时间:2023-11-28 13:17:11 24 4
gpt4 key购买 nike

我正在 View Controller 中实现自定义 UITabBar。我成功地将 UITabBar 委托(delegate)设置为 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)
}

关于ios - 手动设置 UITabBar selected Item 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com