Understanding Delegates - Some confusion...

Discussion in 'iOS Programming' started by Avizzv92, Aug 28, 2009.

  1. Avizzv92 macrumors regular

    Joined:
    Mar 23, 2008
    #1
    I have been reading an iPhone SDK book with a good foundation in Obj-C (Though all my practice came via console programs). I have been doing well understanding the topics presented but I reached a bit of snag when it comes to understanding delegates. It might just be me but I didn't get what the book was trying to convey (seemed a bit vague on the topic).

    This is how I understand delegates and correct me if I'm wrong.

    Say you wanted to display an alert message and the alert message has a button(s). I know that you want to create a delegate to perform actions based on something being done to the button(s). And the class your using as the delegate would need to conform to the <UIAlertViewDelegate> protocol. And in the class definitions you can have a method defined like...
    Code:
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    too do something based on the button that was hit.

    Now the UIAlertView class, does it contain an IBAction that sends
    Code:
    alertView:clickedButtonAtIndex:
    message to its delegate when one of it's buttons have been clicked? That's what I am a little confused on, I'm not understanding how the method in the delegate is being called upon.

    Also would the only times we need delegates (in most cases) would be when you need to do something based on actions performed on user interface objects we don't have physical access to in IB (interface builder) like the buttons on a yet to be created Alert message? And in those cases you do, do the UI parts all have they're own protocols with possible methods that we can use in the delegate? If their would be other common cases where you would need a delegate, what would be some examples?

    Sorry if I'm not very clear on everything (I could just be over thinking things), any help would be appreciated...
     
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Delegation is a way of communication where the delegee and the delegate don't have to know too much about the other one. They both know about the relevant delegate protocol but otherwise don't know that much about the other object.

    Delegation isn't limited to UI classes. NSURLConnection has a delegate. it gets called when relevant actions occur during a connection to a URL.

    Another way to do this kind of communication is by subclassing. In your example you could imagine subclassing an alert class. The various events that occur would result in methods in the subclass being called.

    Cocoa uses delegation in many cases rather than encouraging subclassing.

    You don't really need to understand exactly when your delegate methods will be called or what makes them be called, except in a general way. Yes, events occur in the delegee class that cause it to call the delegate. The delegate protocol is the contract between the classes.
     

Share This Page