I've tried to figure this out I promise! There is a wealth of info out there on this and I'm still awash in a sea of abstract concepts! It's like when I was a kid and nobody could explain to me why a country couldn't just print more money and be really rich. I'm not this retarded with most of this stuff, but for some reason I can't wrap my head around this concept, so would really appreciate if someone could spell it out as patronizingly "talking to a 4 year old" slowly as possible!
I think target-action makes complete sense to me. It is a useful way of allowing a view to talk to a controller, without having to do a whole lot. As far as I can make out, a controller object effectively attaches a listener to the view object so that if a particular event occurs on that view (ie button pushed) it fires the controller method. This may not be not technically accurate, but as an abstract explanation it makes sense to me.
So the sequence is:
- A target method is created in the controller object.
- A view is selected and graphically connected to that method.
- An event happens on that view which fires the method from the controller.
Protocols and delegates have me flummoxed. I know it's something to do with allowing objects to talk to each other, but I tried writing out my (lack of) understanding so far below and just deleted it as I think it's best not to unpick the knots in my current thinking but to just wipe the slate and start afresh. If anybody could kindly spend a little time explaining the purpose of
- Using delegates/protocols as opposed to target/action
- The constituents of the code, and where it lives
- The sequence of events which occur when the process is being used
I would be eternally grateful.
Judging by some of the comments on other explanations, I feel I'm not the only one a little bit lost so hopefully this will be of general use. Thanks so much!
Edit:
Okay as I thought maybe if I just lay out my understanding people can correct me, and it might make this easier.
My sample is taken from the Apple Docs, with a Window as the view object and WindowDelegate as the delegate, where a click on the close window button triggers a "should I close?" message to the delegate.
Constituents of the Code: Window (View) WindowDelegate (View Controller?)
- Declare the protocol which a delegate can use in the interface section of the Window (View).
- Make an instance of the delegate in the Window.
- State that the WindowDelegate implements the Window Protocol, by including : in it's interface. (bit wobbly here?)
- Write the necessary implementation of the methods in the WindowDelegate (View Controller) implementation section.
- On a certain event the Window will send a message to the WindowDelegate with certain information.
- The WindowDelegate will process this and return an answer.
Anywhere along right lines?