Saturday, 3 November 2012

What is Delegation??

Many of the UIKit classes in iOS SDK use the Delegation design pattern. Delegation means that you designate one object to act on behalf of another object. A common example of this is the table view in UIKit. A table view will require an object to act as a delegate to do things like return the number of rows in a section, return a table cell and so on.

Delegation is a nice way to handle this situation. What I do is have the parent table view controller act as a delegate for the child editing view. When a user is done writing a note the editing view controller will send a message back to the parent table view controller. This gives me a chance to update the UI with the new note information.


You are going to need to do these things to use Delegation:

Define a protocol for ChildEditor.
Add a delegate property to ChildEditor that requires the protocol from step 1.
Add a NSIndexPath property to ChildEditor to remember what table view cell the string is displayed in.
When ChildEditor updates a string it must send a message to the delegate property from step 2
ParentTable must adopt the protocol from the step 1.
ParentTable must implement the delegate method that corresponds to the message that will be sent when ChildEditor updates a string (this is when the UI is updated).
Before a new ChildEditor is pushed onto the navigation controller set the delegate property to the ParentTable (you can use the self keyword here).
Before a new ChildEditor is pushed onto the navigation controller set the indexPath property to the property that you get in the didSelectRowAtIndexPath method.

1 comments:

Anonymous said...

great Thanks for tutorial...

 
© Copyright 2035 iOS Developer
Theme by Yusuf Fikri