Minutes and delegates for dummies

I tried to figure it out, I promise! There is a lot of information on this, and I'm still flooded with a sea of ​​abstract concepts! I like it when I was a child, and no one could explain to me why a country cannot just print more money and be really rich. I do not linger on most of these things, but for some reason I cannot plunge into this concept, so it would be very useful if someone could describe it as patronizing "talking to a 4-year-old," as slowly as possible

I think that the targeted action makes complete sense to me. This is a useful way to allow browsing to talk to the controller, without having to do a lot. As far as I can understand, the controller object effectively attaches the listener to the view object, so if a certain event occurs in this view (i.e., a button is pressed), it starts the controller method. This may not be technically accurate, but as an abstract explanation, it makes sense to me.

So the sequence:

  • The target method is created in the controller object.
  • The view is selected and graphically associated with this method.
  • In this view, an event occurs that fires the method from the controller.

The protocols and delegates annoy me. I know that this is due to allowing the objects to talk to each other, but I tried to write out my (lack of) understanding so far and simply deleted it, since I think it’s better not to dissolve the nodes in my current thinking, but just wipe the slate and run again. If someone can kindly spend some time explaining the purpose

  • Using delegates / protocols as opposed to a goal / action
  • Components of the code and where it lives.
  • The sequence of events that occur when using the process

I would be eternally grateful.

Judging by some comments on other explanations, I feel that I'm not the only one who got a little lost, so I hope this will be of general use. Many thanks!

Edit:

Well, as I thought, maybe if I just lay out my understanding, people can fix me, and it can make it easier.

My sample is taken from Apple Docs , with a window as a viewer and WindowDelegate as a delegate, where clicking on the close button of a window causes "should I close?" message to the delegate.

Components of the code: Window (view) WindowDelegate (View Controller?)

  • Declare a protocol that a delegate can use in the interface section of the window (View).
  • Make an instance of the delegate in the window.
  • Indicate that WindowDelegate implements the Window protocol by including an interface in it. (is it a little shaky here?)
  • Write the required implementation of the methods in the implementation section of WindowDelegate (View Controller).
  • In a specific event, the window will send a message to WindowDelegate with specific information.
  • WindowDelegate will handle this and return a response.

Everywhere along the right lines?

+8
design-patterns objective-c protocols delegates
source share
4 answers

There is a whole bunch of things here, and it sounds like you have a few things confused.

First, I suggest you go to the Apple developer site and download the Objective-C: A Primer and Objective-C tutorials. Ignore the goal / action interface wiring material for now, because it seems like you need to understand some of the basics of Objective C. Here you will learn all about protocols, etc. And other object-oriented materials.

Secondly, there are many very good books that will help you step by step in developing the application. Start developing iPhone 4: Learning the iOS SDK is one that is well regarded. This is also where you learn about delegates.

Third, spend some more time on the documentation. there are many articles in the SDK documentation that explain how everything works from the most basic level, as well as on the Apple Developer website.

+3
source share

Delegates exist to help you avoid subclasses.

Subclassification is something you should always avoid, as it is a very tight form of grip. Linking is when two objects depend on each other to work correctly, and the more connections you have, the more difficult it will be to make changes to your program (because whenever you have to change one object, you will be forced to make changes to other objects that depend on it).

A reason subassembly is a form of communication because when you subclass from a superclass, your subclass will depend on the methods of the superclass (which are inherited by the subclasses). Therefore, if you need to change the superclass, then you may have to change all subclasses.

Say that you want to have a bunch of objects that are exactly the same, except that they do with one method. When subclassing, you need to create a bunch of different subclasses and overwrite this one method for all of them, which will be a lot of subclasses (and an undesired connection). There are delegates present. Instead of subclassing a bunch of times, you can simply create one class and create it to own an identifier object of an anonymous type, which will be the delegate object (we will call it a child). This child will have a single unique method that was previously in the parent, but now the parent will initiate the method for the child.

Instead of subclassing a bunch of times, we create a bunch of instances of the same class and provide each other with a delegate object, which we can do, since the type identifier does not indicate what type of object we should have. Every other delegate object implements this method in different ways, and we avoid subclasses.

+2
source share

To understand the delegates, take a look at the delegates provided by Cocoa. For example, NSWindow has a delegate method called - (void) windowWillClose: (NSNotification *) aNotification if you implement this delegation method, you look at the notification to determine which window should be closed and do some cleaning for that window. You are a delegate to the NSWindow object in the sense that you are doing some work for the window object.

All Cocoa delegate methods have names containing words such as “done,” “will,” “must,” etc. They give your code the ability to do special things in addition to the normal things that these objects themselves have done - therefore, you are a delegate to these objects.

0
source share

For delegates you can look at this question .

For protocols, you can check this question or see the documentation .

Hope this helps!

-one
source share

All Articles