简体   繁体   English

无法构造'UITabBarDelegate',因为它没有可访问的初始化程序

[英]'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.

相关问题 无法构建&#39;DepartmentDataDelegate&#39;,因为它没有可访问的初始化程序 - 'DepartmentDataDelegate' cannot be constructed because it has no accessible initializers 无法构造“任务”,因为它没有可访问的初始值设定项 - 'Task' cannot be constructed because it has no accessible initializers 错误:'结果<data, foursquareerror> ' 无法构造,因为它没有可访问的初始值设定项</data,> - Error: 'Result<Data, FourSquareError>' cannot be constructed because it has no accessible initializers Swift:为什么使用MKPointAnnotation而不使用MKAnnotation。 无法构造“ MKAnnotation”错误,因为它没有可访问的初始化程序。 - Swift: why MKPointAnnotation and not MKAnnotation. Errors out with “'MKAnnotation' cannot be constructed because it has no accessible initializers” 为什么我在 Swift 中得到这个“无法构造,因为它没有可访问的初始值设定项” - Why do I get this in Swift “cannot be constructed because it has no accessible initializers” 无法构造枚举,因为它没有可访问的初始化程序 - enum cannot be constructed because it has no accessible initialisers AppDelegate中的UITabBarDelegate - UITabBarDelegate in AppDelegate 类没有初始化程序Swift - Class has no initializers Swift 类&#39;ProductDetailViewController&#39;没有初始化程序 - Class 'ProductDetailViewController' has no initializers “ViewController”类没有初始值设定项 - Class 'ViewController' has no initializers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM