在Swift中使自定义类中的委托方法可覆盖

时间:2015-12-06 04:28:54

标签: ios swift delegates

所以我有一个自定义类,我为它定义了一个委托方法。这是一个简化的例子:

protocol MyDelegate {
    func myDelegateMethod()
}

class Myclass (){
    //other stuff
    func myDelegateMethod(){
        //some operation
    }
}

我想从另一个类重写此方法。我用了

override func myDelegateMethod(){
}

但是我收到了这个错误

  

Method不会覆盖其超类

中的任何方法

我理解这是说超类没有实现这个方法所以我不需要使用override关键字。但在我的情况下,我在超类myDelegateMethod中有MyClass,我确实想要覆盖它。

那么我怎样才能使这个功能可以覆盖?

编辑:

如果您想查看实际代码,请点击此处the gist

2 个答案:

答案 0 :(得分:4)

protocol MyDelegate {
    func myDelegateMethod()
}

这是extension的{​​{1}}。通过提供protocol和空func,它现在是block。您可以在函数中添加一些内容并将其作为默认实现。

optional func

现在编译:

extension MyDelegate {

    func myDelegateMethod() { } // just a default

}

这是一个没有继承或一致性的类。它有一个与委托同名的方法,但两者完全不相关。这很令人困惑。

class Myclass : MyDelegate {

}

这是一个符合协议的类,不需要class Myclass { //other stuff func myDelegateMethod() { //some operation } }

override

这是一个子类,如果你想让函数有不同的主体,它需要class Myclass : MyDelegate { //other stuff func myDelegateMethod(){ //some operation } }

override

答案 1 :(得分:0)

以下是创建自定义委托的示例代码。所以下面有两个viewController(viewController1和viewController2)。在此viewController1将使用自定义委托将数据发送到viewController2: -

<强> // AppDelegate.swift

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    @IBOutlet weak var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        //Created Instance of both view controller
        let vwController1 = ViewController1()
        let vwController2 = ViewController2()
        //Now set Viewcontroller1 to delegate of Viewcontroller2
        vwController1.delegate = vwController2
        vwController1.test()
        return true
    }

<强> // ViewController1.swift

import UIKit
protocol customDelegate{
    //Created Custom Delegate method
    func sendData(dataValue: String) -> String
}
class ViewController1: UIViewController {
    var delegate : customDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    //Below function will send data to ViewController2 using custom delegate
    func test () {
        delegate?.sendData("test")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

// ViewController2.swift

import UIKit
class ViewController2: UIViewController,customDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //Below is the custom delegate method which will invoke by Viewcontroller1 with sending data
    func sendData(dataValue: String) -> String {
        print("\(dataValue) recieved")
        return dataValue
    }
}