Tutorial 2 - Delegates

This is the forum to discuss iOS Apprentice Tutorial 1: Getting Started.

Tutorial 2 - Delegates

Postby alterarch » Sun Apr 22, 2012 4:06 am

Hey,

I have been having trouble understanding the syntax behind delegates. Whenever I try to implement it, I get runtime errors (on all three projects I have started). I also had a few questions about delegates:

1. Is a delegate something that allows you to pass data between two view controllers?
2. Why do you need to make the textField a delegate of the files owner (in tutorial 2)
3. Do delegates only work as the view controller is opening or closing?
4. Why do you put methods in @protocol? Is it so the two classes can share the same method or one method responds to the other view controller?
5. Why do you do both @class and #import "ClassHeaderFile.h"?

Thanks for your help
alterarch
Hacker
 
Posts: 10
Joined: Fri Apr 20, 2012 2:48 am
Has thanked: 1 time
Been thanked: 0 time

Re: Tutorial 2 - Delegates

Postby Hollance » Sun Apr 22, 2012 10:41 am

alterarch wrote:1. Is a delegate something that allows you to pass data between two view controllers?

Yes. If view controller A opens view controller B, then to send data back from B to A you would make A the delegate of B.

2. Why do you need to make the textField a delegate of the files owner (in tutorial 2)

It's the other way around. The File's Owner (which is the view controller) is the delegate of the text field, so that the text field can let the view controller know when interesting events happen.

3. Do delegates only work as the view controller is opening or closing?

No, they can be used at any time. But when the view controller is closing is a good point to let the presenting view controller (A) know that the view controller (B) is closing.

4. Why do you put methods in @protocol? Is it so the two classes can share the same method or one method responds to the other view controller?

The protocol defines what the delegate is actually supposed to do.

5. Why do you do both @class and #import "ClassHeaderFile.h"?

I use @class in the .h file and the #import int the .m file, unless it is required in the .h file. ;-)
Like card games? Play my new game Mahjong Cards for iPad.
User avatar
Hollance
Team Member
iOS Tutorial Team Member
 
Posts: 4745
Joined: Wed Mar 09, 2011 8:33 pm
Location: The Netherlands
Has thanked: 0 time
Been thanked: 660 times


Return to iOS Apprentice Tutorial 1: Getting Started

Who is online

Users browsing this forum: No registered users and 2 guests