![](/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.