[英]'UITabBarDelegate' cannot be constructed because it has no accessible initializers
I came from a background in Android development to the iOS world. 我来自Android开发的背景知识,也涉及过iOS。
I'm trying to avoid those odd patterns (at least for a Android dev) that connects storyboard items directly to my controller (like @IBOutlet). 我试图避免那些将情节提要项直接连接到我的控制器(例如@IBOutlet)的奇怪模式(至少对于Android开发者而言)。
I want to know if it's possible to create an anonymous function to delegate some events of a UITabBar: 我想知道是否可以创建一个匿名函数来委派UITabBar的某些事件:
let tabBarDelegate = UITabBarDelegate {
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
label.text = item.title
}
}
tabBar.delegate = tabBarDelegate
The error I'm facing is this one: 'UITabBarDelegate' cannot be constructed because it has no accessible initializers
. 我面临的错误是这样的:
'UITabBarDelegate' cannot be constructed because it has no accessible initializers
。
I'm really new to this world. 我真的是这个世界的新手。 How can i accomplish that?
我该怎么做?
You can't instantiate a UITabBarDelegate because it is a protocol (similar to an interface in Java), not a class. 您不能实例化UITabBarDelegate,因为它是一个协议(类似于Java中的接口),而不是类。
You have to have one of your classes declare that it implements UITabBarDelegate, then set an instance of that class as the tab bar's delegate. 您必须让您的一个类声明它实现UITabBarDelegate,然后将该类的实例设置为选项卡栏的委托。
Here's a short example: 这是一个简短的示例:
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
Also, I don't believe you can create anonymous classes in Swift. 另外,我不相信您可以在Swift中创建匿名类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.