views:

145

answers:

3

Hi guys,

Simple question, as I am coming from another programming language. In Objective-C, lets say in a controller class I want to separate certain code into its own method, how do I call that method let's say, from viewLoad. As an example, let's say I create a method:

  • (void)checkIfInputCorrect { NSLog(@"text"); }

Now, i wanted to have in a delegate method, call this method. I tried [self checkIfInputCorrect] and get a warning saying Controller may not respond to -CheckIf...

I thought something like checkIfInputCorrect() would work that gives an error as well.

Basically how do you call a method?

A: 

You need to list the method in the interface (ideal) or list the method implementation before the calling method (less ideal) so that the compiler can know that the class responds to the selector before it compiles the calling line.

Martin Gordon
+5  A: 

Add this to your .h file

- (void)checkIfInputCorrect;

Call it with:

[self checkIfInputCorrect];
ACBurk
A: 

To paraphrase Martin,

In your .m file, make sure your method -checkIfInputCorrect is placed so that it's physically above the method that has the line: [self checkIfInputCorrect];

willc2
Ordering is unnecessary, and is the wrong way to fix it - you need to declare a header so taht it knows about it somewhere (either in the .m or more commonly in the .h file). Putting it above is just a fragile way of getting the function declared before it's used.
AlBlue
Well, it's a valid feature of the compiler and appropriate for methods called from within the class. It saves a ton of typing boilerplate which is a constant source of low-level compiler errors as any method signature name change has to be done in two places.
willc2