I know how delegates work, and I know how I can use them.
But how do I create them?
I know how delegates work, and I know how I can use them. But how do I create them? |
||||
|
An Objective-C delegate is just an object that has been assigned as a delegate of another. There's no special process for creating them; you simply define a class that implements the delegate methods you're interested in. (Though with delegates that use a formal protocol, you must declare your delegate to implement that protocol; see below.) For example, suppose you have an NSWindow. If you'd like to implement its delegate's windowDidMove: method, you could create a class like this:
Then you could create an instance of MyClass and assign it as the window's delegate:
On the NSWindow side, it probably has code similar to this to see if the delegate responds to the windowDidMove: message using respondsToSelector: and send it if appropriate.
The delegate property itself is typically declared To define your own delegates, you'll have to declare their methods somewhere. There are two basic approaches, discussed in the Apple Docs on protocols: 1) An Informal Protocol This can be done, as NSWindow does, in a category on NSObject. For example, continuing the example above, this is paraphrased from NSWindow.h:
You would then use 2) A Formal Protocol The newer option is to declare a formal protocol. The declaration would look like this:
This is analogous to an interface or abstract base class, as it creates a special type for your delegate, NSWindowNotifications in this case. Delegate implementors would have to adopt this protocol:
And then implement the methods in the protocol. For methods declared in the protocol as Speed Optimizations Instead of checking whether a delegate responds to a selector every time we want to message it, you can cache that information when delegates are set. One very clean way to do this is to use a bitfield, as follows:
Then, in the body, we can check that our delegate handles messages by accessing our |
|||||||||||||||||||||
|
The approved answer is great, but if you're looking for a 1 minute answer try this: MyClass.h file should look like this (add delegate lines with comments!)
MyClass.m file should look like this
To use your delegate in another class (UIViewController called MyVC in this case) MyVC.h:
MyVC.m:
Implement delegate method
|
|||||||||
|
When using the formal protocol method for creating delegate support, I've found that you can ensure proper type checking (albeit, runtime, not compile time) by adding something like:
in your delegate accessor (setDelegate) code. This helps minimize mistakes. |
|||
|
Maybe this is more along the lines of what you are missing: If you are coming from a C++ like viewpoint, delegates takes a little getting used to - but basically 'they just work'. The way it works is that you set some object that you wrote as the delegate to NSWindow, but your object only has implementations (methods) for one or a few of the many possible delegate methods. So something happens, and It is totally trivial to do this with your own objects, there is nothing special going on, you could for instance have an
The other thing about delegates is that they are not retained, so you always have to set the delegate to |
||||
|
Please! check below simple step by step tutorial to understand how Delegates works in iOS. I have created two ViewControllers (for sending data from one to another)
|
||||
|
I think all these answers make a lot of sense once you understand delegates. Personally I came from the land of C/C++ and before that procedural languages like Fortran etc so here is my 2 min take on finding similar analogues in C++ paradigm. If I were to explain delegates to a C++/Java programmer I would say What are delegates ? These are static pointers to classes within another class. Once you assign a pointer, you can call functions/methods in that class. Hence some functions of your class are "delegated" (In C++ world - pointer to by a class object pointer) to another class. What are protocols ? Conceptually it serves as similar purpose as to the header file of the class you are assigning as a delegate class. A protocol is a explicit way of defining what methods needs to be implemented in the class who's pointer was set as a delegate within a class. How can I do something similar in C++? If you tried to do this in C++, you would by defining pointers to classes (objects) in the class definition and then wiring them up to other classes that will provide additional functions as delegates to your base class. But this wiring needs to be maitained within the code and will be clumsy and error prone. Objective C just assumes that programmers are not best at maintaining this decipline and provides compiler restrictions to enforce a clean implementation. |
|||
|