![](/img/trans.png)
[英]'DepartmentDataDelegate' cannot be constructed because it has no accessible initializers
[英]'UITabBarDelegate' cannot be constructed because it has no accessible initializers
我來自Android開發的背景知識,也涉及過iOS。
我試圖避免那些將情節提要項直接連接到我的控制器(例如@IBOutlet)的奇怪模式(至少對於Android開發者而言)。
我想知道是否可以創建一個匿名函數來委派UITabBar的某些事件:
let tabBarDelegate = UITabBarDelegate {
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
label.text = item.title
}
}
tabBar.delegate = tabBarDelegate
我面臨的錯誤是這樣的: 'UITabBarDelegate' cannot be constructed because it has no accessible initializers
。
我真的是這個世界的新手。 我該怎么做?
您不能實例化UITabBarDelegate,因為它是一個協議(類似於Java中的接口),而不是類。
您必須讓您的一個類聲明它實現UITabBarDelegate,然后將該類的實例設置為選項卡欄的委托。
這是一個簡短的示例:
class MyViewController: UIViewController, UITabBarDelegate {
//all of UITabBarDelegate's methods are optional, so you don't have to implement them
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
}
}
var viewController = MyViewController()
var tabBar = UITabBar()
tabBar.delegate = viewController
另外,我不相信您可以在Swift中創建匿名類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.