You are on page 1of 13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

iOS and the Outdoors


A development blog for the iPhone/iPad and my other adventures

Simple delegate tutorial for ios development


Hello everyone, Im writing this tutorial as an introduction to the delegate design pattern in IOS development. This tutorial is based around a very simple example Ive put together as an iphone project. As I was teaching myself iphone development I avoided properly learning using delegate functions other than getting a table view to work which was a real drag with my projects because creating custom delegate methods is a powerful tool that I should have been using all along. Prerequisites for this tutorial are a basic understanding of UIKIt and communication between view controllers. The example project which you can download at this link: Delegate Example Project Purpose of this example: This example is a bare bones iphone project which contains two view controllers where the root viewcontroller (DelegateExampleViewcontroller) is meant to display a user entered number and a view controller which will be presented as a modal view controller (EnterAmountViewController) and the value entered by the user will be sent to the root view controller through a delegate method. Just running the project will explain better than I can in this paragraph. Lets get started: Open up EnterAmountViewController.h

/ / d e l e g a t et or e t u r na m o u n te n t e r e db yt h eu s e r @ p r o t o c o lE n t e r A m o u n t D e l e g a t e @ o p t i o n a l ( v o i d ) a m o u n t E n t e r e d : ( N S I n t e g e r ) a m o u n t ;
http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/ 1/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

@ e n d

This is the delegate declaration. For our delegate we only have one method which will be sending back to our root view controller an NSInteger which we will get from the user. NOTE: we extend the protocal to be of type NSObject which helps with autocomplete ALSO: the @optional qualifier means that any object which is declared to be a EnterAmountDelegate can implement that function but if the @required is used in its place xcode will require that the delegate implement that function.

@ i n t e r f a c eE n t e r A m o u n t V i e w C o n t r o l l e r:U I V i e w C o n t r o l l e r{ I B O u t l e tU I T e x t F i e l d* a m o u n t T e x t F i e l d ; } ( I B A c t i o n ) c a n c e l P r e s s e d ; ( I B A c t i o n ) s a v e P r e s s e d ; @ p r o p e r t y ( n o n a t o m i c , a s s i g n ) i dd e l e g a t e ; @ e n d

As you can see we declare id which is a reference to the defined delegate. We need to declare this as a property so the root view controller can access it. Next open up EnterAmountViewController.m and specifically look at this function:

( I B A c t i o n ) s a v e P r e s s e d { / / I sa n y o n el i s t e n i n g i f ( [ d e l e g a t er e s p o n d s T o S e l e c t o r : @ s e l e c t o r ( a m o u n t E n t e r e d : ) ] ) { / / s e n dt h ed e l e g a t ef u n c t i o nw i t ht h ea m o u n te n t e r e db yt h eu s e r [ d e l e g a t ea m o u n t E n t e r e d : [ a m o u n t T e x t F i e l d . t e x ti n t V a l u e ] ] ; }
http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/ 2/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

[ s e l fd i s m i s s M o d a l V i e w C o n t r o l l e r A n i m a t e d : Y E S ] ; }

This function is connected to the save button on the toolbar which would be when the user is done entering their number(which doesnt really mean anything). This is the point where were gonna call the delegate function which we declared in the .h. Before we call the function we first check if anyone is listening with
i f ( [ d e l e g a t er e s p o n d s T o S e l e c t o r : @ s e l e c t o r ( a m o u n t E n t e r e d : ) ] )

If someone is listening then we call amountEntered with the integer value of our text field. Next open up DelegateExampleViewController.h The one line to pay attention to is @ i n t e r f a c eD e l e g a t e E x a m p l e V i e w C o n t r o l l e r:U I V i e w C o n t r o l l e r{ where the the view controller is being declared as an EnterAmountDelegate which is saying that it will implement all required delegate functions and may implement some or all of the optional delegate functions. In our example we only have one optional delegate function. Finally up DelegateExampleViewController.m

# p r a g m am a r kE n t e r N u m b eD e l e g a t ef u n c t i o n / / d i s p l a yt h ea m o u n ti nt h et e x tf i e l d ( v o i d ) a m o u n t E n t e r e d : ( N S I n t e g e r ) a m o u n t { a m o u n t L a b e l . t e x t=[ N S S t r i n gs t r i n g W i t h F o r m a t : @ " % i ",a m o u n t ] ; }

This is delegate function being implemented in our view controller. This function will be called from EnterAmountViewController and we know it will contain our user input which we care about. All we are doing is taking that value and displaying it as a string in our UILabel. Thats it. In this tutorial weve gone over a basic example of using a delegate protocal to communicate between two view controllers. This probably could have been accomplished using notifications or another hacky method but by using delegates we have made the view controller much more reusable and organized. I hope this tutorial has helped and if it has I would appreciate a comment or send me an email: Email Me
http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/ 3/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

Again here is the sample iphone project. Delegate Example Project -Ryan

You Are Your Own Gym

This entry was posted in Tutorials and tagged delegate, Development, IOS, ipad, iPhone, iPhone dev, iphone help, tutorial, view controllers on April 12, 2011 [http://www.roostersoftstudios.com/2011/04/12/simpledelegate-tutorial-for-ios-development/] .

43 thoughts on Simple delegate tutorial for ios development

Abdul
April 13, 2011 at 2:30 pm

This is a thing I have to do more research into, many thanks for the article.

Rodrigo
May 7, 2011 at 3:32 pm

Great tutorial, I was struggling with delegation and now you have made it very easy to understand, thanks and please keep posting interesting iOS tutorials.

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

4/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

joshk
May 12, 2011 at 11:57 pm

seems perfect for child communicating with parent scenarios. thanks!

Don Larson
July 6, 2011 at 7:21 pm

Thank you.

Michael
July 19, 2011 at 3:19 am

Ive been struggling to really get delegates and this really made it clear. Thanks.

Ryan

Post author

July 19, 2011 at 4:55 am

No problem, Im glad this helps. I wish I found something like this a little sooner when I didnt really understand it.

haersk
July 28, 2011 at 1:17 am

Thx for a great tut! Helped me a lot!

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

5/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

Cora
August 5, 2011 at 7:45 am

This was very helpful, thank you very much!

artist
August 14, 2011 at 7:12 pm

thanks a lot! this is very cool and really helpful!!! :)

Sax155
October 25, 2011 at 9:02 am

Thanks, it was pretty helpful!! :)

7
January 17, 2012 at 2:23 pm

It was pretty helpful, thanks!

scott.stephen.74@gmail.com
January 19, 2012 at 11:01 pm

Thanks for this tut.


http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/ 6/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

I have read a lot of different ones for delegates, but this one is the clearest and simplest of them all. Cheers

Ghalib Awab
April 2, 2012 at 1:39 pm

Awesome tutorial man !!!! Very well and clearly explained in very simple way !!! Thanks a lot.

sebin
April 3, 2012 at 8:27 am

great tutorial with nice example.keep it up dude

Raimondo De Rose
April 7, 2012 at 7:15 pm

5 stars!!

Raimondo De Rose
April 7, 2012 at 7:15 pm

5 stars!!

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

7/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

Go Inspired
April 24, 2012 at 6:18 pm

Cool! Just what I was looking for! Thanks

Manuel
April 24, 2012 at 6:19 pm

Awesome!! Thanks! Helped me a lot!

Anonymous
May 17, 2012 at 10:57 am

Awesome Tutorial.this is the simplest one of allThanks for it..keep doing the great job man!!!!!

Pingback: How to pass value to a variable defined in parent class from child class? | PHP Developer Resource

Bernd
June 7, 2012 at 11:25 am

Wow, thanks so much for this explanation and example on delegation. This helped me a lot to move on with my project!

Colm Brazel
June 7, 2012 at 3:59 pm

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

8/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

Well explained. Thanks!

Surendra kunwar
June 12, 2012 at 6:11 am

hello friends i am verry congused in deleget .h/.m file can i edit this files and if i edit thn when this editing possible

don
June 18, 2012 at 9:54 am

Nice Tutorial..

SparkyNZ
June 18, 2012 at 7:59 pm

Thanks so much for this. I have been scouring the internet, looking through loads of books an I couldnt find a simple example of how all of this fits together until I read your example. Makes sense now.. I do find it uncomfortable having mention of a parents method inside the childs interface .h file but Im sure its something I can live with.. I just find Obj-C strange overall.. my fault from coming from Windows MFC background..

Memo
June 27, 2012 at 1:24 pm

Thnx ,great tuto

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

9/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

geenieWanted
June 28, 2012 at 8:45 am

You are awesome! I have spent some hours just to understand the concept of custom delegate. You made it so simple! 5 stars for you Mr. Superstar!

David Smith
June 29, 2012 at 10:54 pm

A really good example. You create a delegate in your object to allow other objects to register to the delegate method(s). Which are callback methods. When the delegate processes data/info. it lets the registered objects that new data is ready.

KingCrawlerX
July 26, 2012 at 10:35 pm

Excellent explanation. So concise. All the books teach you is how to use provided delegates. Now my view controllers can talk to each other! And now I wont have to abuse singletons and notificationCenter so much LOL again. Thanks so much and good work!

prasad
August 4, 2012 at 9:30 am

great tutorial with nice example.keep it up dude

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

10/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

Roberto
August 9, 2012 at 6:45 pm

In this case what is the difference between a normal property variable (IBOutlet UITextField *amountTextField;) and this delegate implementation?! What are the advantages of using it?! Thanks for your tutorial!

admin

Post author

August 11, 2012 at 5:33 pm

Well they are two separate things entirely. An IBOutlet is just the connection between your code implementation and Interface Builder. From an IBOutlet you can get a value from whatever youre connecting to but a delegate implementation is more about events and having the parent in the relationship not have to worry about when to get a value or handle an event but just listen to it. I hope that makes sense.

Garfield
September 23, 2012 at 7:32 am

Its amazing for me to have a web page, which is beneficial in support of my knowledge. thanks admin

barn.gumbl
September 29, 2012 at 7:44 pm

it seems that its a callback not a delegate, please correct me if i am mistaken

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

11/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

Pingback: developer | Pearltrees

laxman
October 17, 2012 at 12:37 pm

Its helpful source

Kostas
October 22, 2012 at 7:58 pm

Hey, thanks for the tutorial, it really helped me. But, I think the id delegate; declaration is not necessary, since you declare a property. If you comment it, it works pretty fine.

admin

Post author

October 22, 2012 at 8:14 pm

You are right about not needing the declaration, this was older and I will clean it up. Im glad it helped you out.

Yoga
October 23, 2012 at 12:55 pm

Great post! Simple and straight to the point :D

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

12/13

Ngy 21 thng 1 nm 2014

Simple delegate tutorial for ios development | iOS and the Outdoors

Eloy
November 6, 2012 at 10:49 am

Thanks for this tutorial, its very usefull.

murali
November 9, 2012 at 7:15 am

simple and straight way to understading the point

sidd
December 5, 2012 at 7:36 am

awesome article thnx vry much

jilmon
December 13, 2012 at 9:45 am

simple and clear.helped me a lot..thanks bro

Comments are closed.

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

13/13

You might also like