Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rich1812

macrumors regular
Original poster
Oct 4, 2015
117
5
earth
Hello, I am very foggy of the concept of a delegate, the terminologies got me confused. For example, I have two classes. ClassA and ClassB, this is outline of the delegate as below:
Screen Shot 2017-06-30 at 07.55.30.png


Few questions:
1. Is my outline correct?
2. Which one is the delegate class? ClassA or ClassB? Which one is the parent? ClassA or ClassB?
3. When pass data form ClassB to ClassA, many of the examples I saw were using prepare and segue. What if I do not want to use prepare and segue?

Thank you.
 
Last edited:

Mascots

macrumors 68000
Sep 5, 2009
1,665
1,415
A protocol is more-or-less a reference of expected properties or methods. Delegation, in that context, means one object uses those references to have another object do work without (the first) knowing explicitly how it is done.
  1. On ClassA and ClassB, your methods are outside of the actual classes; protocolFunc would need to be inside of Class A in order to satisfy the protocol and xyz would need to be inside of ClassB to access the delegate property.
  2. Rather than thinking parent/child, think Receiver and Sender. ClassB is the sender of commands to delegate, ClassA is the receiver of those commands.
  3. If you do not want to use Storyboard segues, you can initiate your classes from Storyboard, a Nib, or in your code then add them to to your stack manually.
 

rich1812

macrumors regular
Original poster
Oct 4, 2015
117
5
earth
Mascots thank you for your reply.
Almost all the search I found online, it was written in the above format (for the lack of word.) the protocol outside of the class. And a lot of examples further complicate with use UIViewController added segue etc. Got my head spinning. I really only need a pure clean cut example in its simplest form to understand the concept. Could you please give me a simple example of two geric classes ClassA and ClassB, how the structure of each class is and how the data is passed? That will really help.
Thank you very much.
 

Mascots

macrumors 68000
Sep 5, 2009
1,665
1,415
Code:
protocol ModelDelegate: class {
    func save(text: String)
    func cancel()
}

class EventResponder {
    enum EventType {
        case save(title: String)
        case cancel
    }
   
    weak var delegate: ModelDelegate?
   
    init(delegate: ModelDelegate?) {
        self.delegate = delegate
    }
   
    func eventTriggered(type: EventType) {
        switch(type) {
        case .cancel:
            self.delegate?.cancel()
        case .save(let title):
            self.delegate?.save(text: title)
        }
    }
}

class Model: ModelDelegate {
    func save(text: String) {
        print("Save The Text: \(text)")
    }
   
    func cancel() {
        print("Do something to bail")
    }
}


let model = Model()
let responder = EventResponder(delegate: model)

responder.eventTriggered(type: EventResponder.EventType.save(title: "Save String"))

Here is a simple expression of the delegate pattern.
Edit: I updated it to flesh it out a little bit more as an example.
 
Last edited:
  • Like
Reactions: jgaz

KALLT

macrumors 603
Sep 23, 2008
5,361
3,378
In a nutshell:
  1. Create a class protocol with the methods and properties that you want the delegate to implement.
  2. Create a ‘delegate’ property with the protocol as the type on the class that has to communicate with the delegate.
  3. Make the class that should act as delegate conform to the protocol.
  4. Assign the delegate object to the ‘delegate’ property.

Step 4 of the delegate pattern is somewhat more complicated with Storyboard, as you aren’t initialising the child controllers yourself. This is handled by Storyboard. Segues give you the opportunity to establish relationships between view controllers. For instance, when the prepareForSegue method is called on the parent controller, it can use it to set the delegate property on the child controller.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.