开发者

How to call a method inside another method in Objective-C

I'm new to Objective-C.开发者_如何学JAVA

If I wrote this method declaration in .h

-(void)myMethod;

and this implementation in .m

-(void)myMethod{
    NSLog(@"This is myMethod");
}

How can I call it in my class' viewDidLoad method?

Thank you.


Simply just use object "self"

[self myMethod];


Assuming that -viewDidLoad is in the same class, use

[self myMethod];

self here is an automatic reference to the current object instance. If you want to call a method on another object stored in a pointer otherObj, it would be

[otherObj myMethod];


Assuming that you're calling myMethod on the same class as what implements viewDidLoad:

- (void)viewDidLoad {
    //...other code
    [self myMethod];
    //...other code
}

If you are having trouble with basic Objective-C though, I'd strongly suggest to either get a decent book on Objective-C, such as:

  • Programming in Objective-C 2.0 by Stephen Kochan
  • Cocoa Programming for Mac OS X by Aaron Hillegass
  • iPhone Programming: The Big Nerd Ranch Guide by Joe Conway & Aaron Hillegass

…or at least read a good beginners tutorial, such as:
http://www.cocoadevcentral.com/d/learn_objectivec/

There are quite a few more helpful beginners tutorials by Scott Stevenson here:
http://www.cocoadevcentral.com/


[self methodname]; 

will work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜