Results 1 to 4 of 4

Thread: Why use delegates?

  1. #1

    Member Since
    Mar 04, 2010
    Why use delegates?
    I'll bet money that I'm missing something here, but I don't quite understand the point of jumping through hoops for delegates. I just learned how to use them, but what I don't understand is why.

    Instead of doing all the set up work, why not just have a certain view controller assign itself as some sort of property (aside from the delegate property) and then just call a method from the view controller using the property? Like:

    //In the first view controller which is a subclass of UIViewController
    //(Assume SecondViewController is another subclass of UIViewController)
    SecondViewController *myViewController = [[UIViewController alloc] init];
    myViewController.ObjectImGoingToSendAMessageTo = self;
    [[self navigationController] pushViewController:myViewController animated:YES];
    [myViewController release];
    Then in the SecondViewController class set up the ObjectImGoingToSendAMessageTo ivar and then just have a method like this:

    - (void)somethingHappened {
    [ObjectImGoingToSendAMessageTo doSomethingCool];
    Wouldn't this accomplish the same thing (i.e. being able to send some message from the second view back to the first view)? Or is there something special about using protocols and delegates that I'm not understanding?

  2. #2

    nabl's Avatar
    Member Since
    Dec 13, 2007
    United States of America
    I'd try to explain it, but I think Apple has already done an excellent job: read this (also check out the Definitive Discussion at the bottom). Basically it helps in separating code among objects, making more generic (reusable!) code, and in keeping code MVC-compliant.

  3. #3

    xstep's Avatar
    Member Since
    Jun 25, 2005
    On the road
    What you have set up is a delegate. Just to note, the delegate ivar is set as type 'id'.

    One reason to setup the protocol is so that compiler doesn't spit out warnings that the method call on the delegate isn't found. Another reason is so that your object doesn't have to know any further details about the delegate; you don't need to import the .h file from the delegate to clear those warnings. This allows many different objects to be a delegate to your object. Only one at a time though.

    Say I have an object that contains coordinate data used to draw a map of the world. It does't know how to draw though. I also have a window where I want to draw a view and a choice between a Cylindrical Projection map and a Interrupted Projection map. These maps draw very differently, but can use the same data. The window may set up the appropriate view and set the delegate of the data to that view and then send a draw message to the data object. Since the data has a delegate that knows how to draw it will send a "drawWithData: sender" message to the map so that map will know where to get the data. It seems a bit convoluted, but it allows me to use many different map types and easily swap them out at run time.

    If you wanted two maps up at the same time using the same data, then you would look at NSNotification.
    CameraTime - Time lapse photography for novice and advanced users.

    When asking questions, post the version of your software. You'll receive better answers.

    Please post your results to the thread as it is good feedback.

  4. #4

    Member Since
    Mar 04, 2010
    @xstep: That makes a lot of sense, thank you!

    @nabl: Unfortunately that link only explains what delegates are rather than why to use them. But I appreciate the time you took to get me the link. ^_^

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calendar delegates cal showing exclamation point
    By ebsseattle in forum macOS - Apps and Games
    Replies: 1
    Last Post: 05-14-2015, 06:28 PM
  2. Outlook Software -Email Delegates
    By rachel.berg in forum Other Hardware and Peripherals
    Replies: 0
    Last Post: 11-27-2013, 10:54 AM
  3. Replies: 1
    Last Post: 04-06-2012, 06:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts