开发者

Objective-C: call a method you just created

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?


Add this to your .h file

- (void)checkIfInputCorrect;

Call it with:

[self checkIfInputCorrect];


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.


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];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜