我有一个UIBarButtonItem子类," NavBarButtonItem",在Swift 3.0中定义,但我不确定我是否正确设置了便利初始化程序。这个初始化程序似乎在其他Swift类中运行良好:
let nextBarButtonItem = NavBarButtonItem.init(self, "Next", #selector(pressedNextButton))
然而,在Obj-C中,Xcode向我展示了以下(它缺少参数标签):
self.nextButton = [NavBarButtonItem alloc] init:<#(id<NavBarButtonDelegate> _Nonnull)#> :<#(NSString * _Nonnull)#> :<#(SEL _Nonnull)#>
我完成了它:
self.nextButton = [[NavBarButtonItem alloc] init:rvc :2 :@"Next" :@selector(doneButtonTapped:)];
我之前没有在Swift中设置初始化程序,因此怀疑可能存在错误但应用程序编译。这是Swift子类:
MyViewController.swift
// this file contains other classes that precede subclass NavBarButtonItem
..
@objc protocol NavBarButtonDelegate {
func pressedNextButton()
}
@objc class NavBarButtonItem: UIBarButtonItem
{
private var delegate: NavBarButtonDelegate?
init(_ delegate: NavBarButtonDelegate){
self.delegate = delegate
super.init()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
convenience init(_ delegate: NavBarButtonDelegate, _ title: String, _ selector: Selector)
{
self.init(delegate, wtfProperty)
self.title = title
self.action = selector
}
}