-3

I am new to Objective-C and i am trying to understand this

My question concerns:

[delegate sayHello:self];

As we know delegate is property. So how can it call the sayHello method of this protocol:

CustomClass *custom = [[CustomClass alloc] init]; // assign delegate

custom.delegate = self;

[custom helloDelegate];

What does the above line mean?

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
kumar0981
  • 39
  • 2
  • 7
  • http://www.tutorialspoint.com/ios/ios_delegates.htm – Vijay yadav May 06 '15 at 13:03
  • possible duplicate of [How do I create delegates in Objective-C?](http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c) – Preson May 06 '15 at 13:08
  • the `delegate` has been set for the `CustomClass` then the `–helloDelegate` calls back the delegate class's `–sayHello:` method, which implementation is optional in the poor tutorial you are reading, so if you forgot to implement the `–sayHello:` method, your application will crash. use tutorials from more reliable sources, not every keen volunteer who has a nice website teaches you properly. – holex May 06 '15 at 13:54

1 Answers1

0

delegate property is type of Id so it can store any kind of object in it.

so when you write delegate=self means your delegate contains current class's reference and when you call method using delegate it will call method implemented in current class.

Suppose we have two controller A and B.

Now You create Protocol and delegate property in B and Called some method of Protocol from Class B

suppose method name myMthod()

Now Delegate property of class B has reference of Class A

You have implemented the Protocol method in Class A.

Now If you want to call myMethod from Class A you will write [self myMethod];

If you call same method using delegate in Class B

[delegate myMethod];

and before this statement delegate=self is execute in Class A so there is no difference between above 2 Statements.

delegate = self // Statement execute in Class A // Delegate Has Class A's ref

now if you Call [delegate myMethod]; or [self myMethod]; both are same.

please refere this link for more details.

Malav Soni
  • 2,739
  • 1
  • 23
  • 52