Delegate in iOS

Discussion in 'iOS Programming' started by rich1812, Jun 30, 2017.

  1. rich1812, Jun 30, 2017
    Last edited: Jun 30, 2017

    rich1812 macrumors member

    Joined:
    Oct 4, 2015
    Location:
    earth
    #1
    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.
     
  2. Mascots macrumors 65816

    Mascots

    Joined:
    Sep 5, 2009
    #2
    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.
     
  3. rich1812 thread starter macrumors member

    Joined:
    Oct 4, 2015
    Location:
    earth
    #3
    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.
     
  4. Mascots, Jun 30, 2017
    Last edited: Jun 30, 2017

    Mascots macrumors 65816

    Mascots

    Joined:
    Sep 5, 2009
    #4
    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.
     
  5. KALLT macrumors 601

    Joined:
    Sep 23, 2008
    #5
    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.
     

Share This Page