How do I implement a custom delegate to a class in iOS?

How do I implement a custom delegate to a class in iOS?

Accepted Reply

How do I implement a custom delegate to a class in iOS?

There’s basically three steps:

  1. Define a delegate protocol:

    protocol MyDelegate : AnyObject {
        func someMethod()
    }

    .

  2. Define a delegate property in your class:

    class MyClass {
        weak var delegate: MyDelegate? = nil
    }

    .

  3. Call that delegate:

    self.delegate?.someMethod()

    .

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

How do I implement a custom delegate to a class in iOS?

There’s basically three steps:

  1. Define a delegate protocol:

    protocol MyDelegate : AnyObject {
        func someMethod()
    }

    .

  2. Define a delegate property in your class:

    class MyClass {
        weak var delegate: MyDelegate? = nil
    }

    .

  3. Call that delegate:

    self.delegate?.someMethod()

    .

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"