Delegate

Discussion in 'iOS Development' started by Blank6, May 2, 2010.

  1. Blank6

    Blank6 New Member

    Joined:
    Sep 8, 2009
    Messages:
    59
    Likes Received:
    0
    Device:
    3G iPod touch
    Hey guys,
    I will give my question as an example: (not a real case)
    I have a traffic light and cars in my program, as a good OOP programmer I would like my cars to sign up at the traffic light as Objects who want to know that the light changed - delegates in short.

    In C# I would create a delegate object for the traffic light, the cars would sign to that delegate, and when the time comes the delegate just signals everybody who signed up for it.

    By what I read about Objective C, all you can do is add a "setObject" method in the traffic light, the cars will call this method and the method will save them aside in an NSMutableArray.
    When the time comes, it will run in a foreach loop and call a method in the car's object. "Traffic changed"

    My question:
    C# conceals all this implementation, there is no such thing in Objective C?
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    have a look at NSNotificationCenter.

    Any and all car objects can "sign up" for notifications, and the traffic light object can post a notification, without knowing exactly what objects are listening.
  3. Blank6

    Blank6 New Member

    Joined:
    Sep 8, 2009
    Messages:
    59
    Likes Received:
    0
    Device:
    3G iPod touch
    Sounds like exactly what I need!
    I missed that on my search in the web, thanks[:
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
  5. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I beg to differ.

    The OP's question:
    I assume(d) that he was referring to
    By referring the OP to NSNotificationCenter, I answered his question. "My" solution is, virtually (and ignoring differences in the frameworks and languages), the same as what is described in the above quote.

    While your response provided equal functionality, it's not exactly what the OP was asking for. (I gathered from his post that the he wanted to do something OTHER than have the traffic light object hold an array of car references and call a method on each when necessary).

Share This Page