-1

I am looking at this question (the answer with 179 upvotes - How do I create delegates in Objective-C?) and need help understanding this:

#import "MyClass.h"
@implementation MyClass 
@synthesize delegate; //synthesise  MyClassDelegate delegate

- (void) myMethodToDoStuff {
    [self.delegate myClassDelegateMethod:self]; //this will call the method implemented in your other class    
}

@end

What does 'myMethodToDoStuff' do? Does it have to be called somewhere in MyClass.m?

Community
  • 1
  • 1
cdub
  • 24,555
  • 57
  • 174
  • 303

2 Answers2

1

"myMethodToDoStuff" is a instance method of MyClass. And yes you need to call that method whenever you want to call following delegate Method.

-(void) myClassDelegateMethod: (MyClass *) sender{     
 // Do stuff  
 }

Note - You can call it on Button click like-

-(IBAction)TestButtonTapped:(id)sender{ 

 [self myMethodToDoStuff];

 }
prema janoti
  • 159
  • 1
  • 4
-1

myMethodToDoStuff function is your own function which you will call whenever you need to execute the delegate's function.

Charith
  • 88
  • 3
  • 1
    This is incorrect. `myMethodToDoStuff` is not a function it is a method. if it was a function then it would be declared something like `void myMethodToDoStuff();` but it isn't it is declared as a method like `- (void)myMethodToDoStuff;` There is a difference between functions and methods you should learn them – Popeye Oct 16 '14 at 08:24
  • 1
    Any "function" in a class is a "method". Anyway, the op just wanted to know what myMethodToDoStuff was going to do. – Charith Oct 16 '14 at 08:43
  • 1
    No it's not there is a different between a function and a method. Have a read of http://stackoverflow.com/questions/6672352/objective-c-difference-between-functions-and-methods – Popeye Oct 16 '14 at 08:46
  • 1
    On the right abstraction levels, functions and methods are the same. Complaining about someone using the wrong term in a situation where it doesn't make any difference doesn't help. – gnasher729 Oct 16 '14 at 11:43
  • This is the right answer but it's being picked apart for the tired old function/method argument??? – Hot Licks Oct 16 '14 at 12:09
  • @HotLicks When I first started I would do the same thing and refer to them as functions instead of methods I would get loads of comments and downvotes saying my answer was wrong just because I used function instead of method. So the way I see it now is that it can't be one rule for one person and another rule for another, I gave the opportunity (as I normally do) to correct which it would have taken 2 seconds to do but they haven't. So the comments in my opinion are perfectly valid. – Popeye Oct 17 '14 at 07:37
  • @Popeye - I guess I'm less didactic on that point, having programmed in FORTRAN, COBOL, Modula2, Pascal, C, Java, and several others I've forgotten. It's hard to remember what term is used by what language, and, in most languages, there is no distinction to be made anyway, or if there is it's not critical to understanding, but only to the parser. – Hot Licks Oct 17 '14 at 10:57