Using Protocols to call a Function in Another Class

时间:2018-03-22 23:32:50

标签: swift4 xcode9

I need to call a function named xyz, which is located in my ViewController class, and call it in my Cell class. This is the code that I use: ViewController

    import UIKit
    protocol abc {
       func xyz()
    }

    class ViewController: UIViewController, abc {
     func xyz() {
       print("Hi")
     }
     override func viewDidLoad() {
      super.viewDidLoad()
      let obj = Cell.init()
      obj.delegate = self
     }
    }

Then for my Cell class I use

    var delegate: abc?

    func update() {
    self.delegate?.xyz()
    }

Thanks

0 个答案:

没有答案