Protocols and Delegation

Discussion in 'iOS Programming' started by nixesmixes, May 19, 2010.

  1. nixesmixes macrumors newbie

    Feb 4, 2010
    Thank you for your responses!

    When a delegate object conforms to another object's delegate protocol is it safe to assume that the methods implemented from the protocol are called automatically called from within the system?

    For example, I have a delegate class called myDelegate that conforms to the UITextViewDelegate protocol. I implement the method textViewDidChange from the UITextViewDelegate protocol. When the user enters text into the UITextView is it safe to assume that my textViewDidChange method will be called automatically from within myDelegate, i.e. message sent to myDelegate object? The book I am studying does not make the point clear and I am having a hard time wrapping my mind around the concept of methods being called for me. Setting up an action makes sense to me because I have connected an UI object with the method from within my controller. In this case I have not done anything so I am a bit confused here. Any help would be greatly appreciated.

    Thank you!

    Edit: I think I am beginning to understand the answer. It appears that every (or is it only some) object has a delegate property. You set that delegate property to point to the the delegate object. So it appears that the system understands or is looking for certain events. When that event occurs it searches the delegate object for the associated method. If the method has been implemented by the delegate object the method will be called. If the method has not been implemented then nothing happens? I am still asking here because I am not sure if this is an accurate description of the process or not. Any and all help is greatly appreciated.
  2. Simple2Do macrumors newbie

    May 18, 2010
    Northern Ireland
    That pretty much sums it up perfectly!

    The only correction I would make is that not every object has a delegate property. Only those objects which are delegators will have a delegate property i.e. UITextField. If you look up the Apple documentation for the object you can see if it has a delegate property. It will also tell you the protocol that it conforms to, i.e. the UITextField documentation defines a delegate which conforms to the UITextFieldDelegate protocol....

    @property(nonatomic, assign) id<UITextFieldDelegate> delegate

Share This Page