我在委托中有一个名为selectionDidFinish(controller:)
的方法来解除委托给出的viewController。所提出的控制器采用我的Dismissable
协议,其UIBarButtonItem
附加了一个操作,应该调用selectionDidFinish(controller:)
方法,但它会给我" '#selector'的争论没有'引用'初始化器或方法"错误。
错误出现在UIViewController
:
class FormulaInfoViewController: UIViewController, Dismissable {
weak var dismissalDelegate: DismissalDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Xcode doesn't like this selector
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(dismissalDelegate?.selectionDidFinish(self)))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我已确认dismissalDelegate
已正确设置为FormulasTableViewController
,因此我无法理解为什么它无法看到dismissalDelegate?.selectionDidFinish(self))
。
我提交的UIViewController
的相关代码是:
class FormulasTableViewController: UITableViewController, DismissalDelegate {
let formulas: [CalculationFormula] = [
CalculationFormula.epley,
CalculationFormula.baechle,
CalculationFormula.brzychi,
CalculationFormula.lander,
CalculationFormula.lombardi,
CalculationFormula.mayhewEtAl,
CalculationFormula.oConnerEtAl]
override func viewDidLoad() {
super.viewDidLoad()
let liftInfoImage = UIImage(named: "info_icon")
let liftInfoButton = UIBarButtonItem(image: liftInfoImage, style: .Plain, target: self, action: #selector(self.segueToFormulaInfo(_:)))
self.navigationItem.rightBarButtonItem = liftInfoButton
}
func selectionDidFinish(controller: UIViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let nav = segue.destinationViewController as! UINavigationController
let vc = nav.topViewController as! Dismissable
vc.dismissalDelegate = self
}
func segueToFormulaInfo(sender: UIButton) {
performSegueWithIdentifier("segueToFormulaInfo", sender: self)
}
}
我已经完成了有关如何使用#selector
的各种研究,我认为this post有所有答案,但它没有。
我已经试过这个可以拒绝的协议,有没有将它暴露给@objc
:
@objc protocol Dismissable: class {
weak var dismissalDelegate: DismissalDelegate? {
get set }
}
我也尝试过我的DismissalDelegate协议:
@objc protocol DismissalDelegate : class {
func selectionDidFinish(controller: UIViewController)
}
extension DismissalDelegate where Self: UIViewController {
func selectionDidFinish(viewController: UIViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
我无法将我的协议扩展程序公开给@objc
- 这就是为什么这不起作用的原因?我的#selector
真的是问题吗?它与我的协议有关吗?
编辑:最后修复
根据接受的答案,我添加了一个解雇的功能:
func dismiss() {
dismissalDelegate?.selectionDidFinish(self)
}
然后像这样调用选择器:
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(dismiss))
答案 0 :(得分:3)
您不能让action
参数指定任意表达式。它需要调用特定的选择器 - 该类中的类和函数。
您需要在类中创建一个调用委托方法的函数:
class FormulaInfoViewController: UIViewController, Dismissable {
weak var dismissalDelegate: DismissalDelegate?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(FormulaInfoViewController.selectionDidFinish)
}
@objc func selectionDidFinish() {
self.dismissalDelegate?.selectionDidFinish(self)
}
}