I need to know about the usage of delegate methods in Objective-C. Can anyone point me to the correct source?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
If the object(s) in question has its
delegate
assigned to a class you wrote, say acontroller
then the methods defined for being that object's class'sdelegate
methods must be implemented by the assigned class. This allows you to effectively control the behavior of the object without sub-classing the object's class in order to override behavior that would likely necessitate an amount of duplicating behavior. It's one of the cleaner parts of the cocoa touch design.This is something you should pick up in the first couple of intros and tutorials to cocoa touch. Like this tutorial from Cocoa is my Girlfriend. In fact they made the
delegate
explanation a big bold heading.You will want to declare a delegate protocol for your class. An example of a delegate protocol and interface for class
Foo
might look like this:Don't forget to synthesize your properties in the
@implementation
.What this code did was declare a protocol called FooDelegate; a class that conforms to this protocol would be declared like
@interface SomeClass : SuperClass <FooDelegate> {}
. Because this class conforms to the protocolFooDelegate
, it now gets to implement the methods underFooDelegate
(to require that these be implemented, use@required
instead of@optional
). The last step is for aFoo
object to be instantiated in the class that conforms toFooDelegate
, and for thisFoo
object to have its delegate property set:Now, your class is prepared to receive messages from
Foo
objects that have their delegates set correctly.Delegates are very useful to control transfer within the array of view controllers in app manually. Using delegates you can manage the control flow very well.
here is small example of own delegates....
SampleDelegate.h
also add above DelegateName in Delegate Reference < >
ownDelegateAppDelegate.h
ownDelegateAppDelegate.m
HomeViewController.h
HomeViewController.h
To start, you can take a look at what Apple has to say about delegate methods. The documentation provides some well written information about what delegation is all about, and explains both how to use AppKit classes that define and support a delegate and how to code delegate support into one of your own objects.
See Communicating With Objects
(If you're interested in coding your own delegate support, skip down to the "Implementing a Delegate for a Custom Class" section.)
The most significant aspect to take away from delegate methods is that they enable you to customize and affect the behavior of an object without the need to subclass it.
Hope that helps you get started.