ios – 在子类内部实现协议方法

ios – 在子类内部实现协议方法,第1张

概述请考虑通过 Cocoapods包含的模块中的以下代码(我使用Eureka,但问题不应与此相关): open class FormViewController : UIViewController { }extension FormViewController : UITableViewDelegate { func tableView(_ tableView: UITableV 请考虑通过 Cocoapods包含的模块中的以下代码(我使用Eureka,但问题不应与此相关):
open class FormVIEwController : UIVIEwController {  }extension FormVIEwController : UItableVIEwDelegate {      func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell {    // some default implementation  }  /// some other UItableVIEwDelegate methods follow,but _NOT_ willdisplayCell}

我试图继承FormVIEwController并为tableVIEw添加一个实现(_:willdisplay:forRowAt :)(这是UItableVIEwDelegate的可选方法).此方法在原始FormVIEwController中没有实现:

import UIKitimport Eurekaclass MyVIEwController: FormVIEwController {    overrIDe func vIEwDIDLoad() {        let tableVIEw = UItableVIEw(frame: self.vIEw.bounds,style: .grouped)        tableVIEw.delegate = self        tableVIEw.dataSource = self        self.tableVIEw = tableVIEw        self.vIEw.addSubvIEw(self.tableVIEw!)        super.vIEwDIDLoad()        form = Form()        let section = Section()        section.append(EmailRow(){            
extension FormVIEwController : UItableVIEwDelegate {        func tableVIEw(_ tableVIEw: UItableVIEw,forRowAt indexPath: IndexPath) {        // you can leave the implementation blank if you want    }}class MyVIEwController: FormVIEwController {    overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,forRowAt indexPath: IndexPath) {        print("CALLED!!")    }}
.tag = "email"
extension FormVIEwController : UItableVIEwDelegate {        // no willdisplayCell method}class MyVIEwController: FormVIEwController {    func tableVIEw(_ tableVIEw: UItableVIEw,willdisplayCell cell: UItableVIEwCell,forRowAtIndexPath indexPath: NSIndexPath) {        print("CALLED!!")    }}
.Title = "Email" }) form += [section] } func tableVIEw(_ tableVIEw: UItableVIEw,willdisplay cell: UItableVIEwCell,forRowAt indexPath: IndexPath) { print("NOT CALLED") } overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,dIDSelectRowAt indexPath: IndexPath) { print("will be called") } overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell { print("GETS CALLED") return super.tableVIEw(tableVIEw,cellForRowAt: indexPath) }}/// somewhere else:self.navigationController?.pushVIEwController(MyVIEwController(),animated: true)

使用此设置,将调用tableVIEw(_:cellForRowAt :)的覆盖版本(意味着正确设置了tableVIEw.delegate).不会调用tableVIEw(_:willdisplay:forRowAt :).只要我在Eureka中添加一个空白实现,我就可以覆盖该方法.

问题:为什么Swift在超类中没有默认实现时不使用该方法?

解决方法 我自己也注意到了这个问题,这对我来说似乎是一个错误.你有两个选择.

首先,您可以在扩展中实现委托方法,并在子类中覆盖它.这将确保调用该方法.

其次,您可以使用pre-Swift 3表示法声明该方法,它将起作用.这部分对我来说也是一个错误(或同一个BUG的所有部分).我不推荐这个选项,因为它可能会在未来的Swift或Xcode版本中发生变化,并且通常会感觉很乱.

编辑:OP将UItableVIEwDelegate方法放在扩展中的事实不会导致问题.即使类本身对委托进行了decalres,问题仍然存在.

总结

以上是内存溢出为你收集整理的ios – 在子类内部实现协议方法全部内容,希望文章能够帮你解决ios – 在子类内部实现协议方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1103456.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存