user765110
user765110

Reputation: 83

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

I'm new to Objective-C.

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.

Upvotes: 4

Views: 8116

Answers (4)

jawed
jawed

Reputation: 41

[self methodname]; 

will work.

Upvotes: 2

Regexident
Regexident

Reputation: 29552

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:

…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/

Upvotes: 2

Cyprian
Cyprian

Reputation: 9453

Simply just use object "self"

[self myMethod];

Upvotes: 10

Seamus Campbell
Seamus Campbell

Reputation: 17906

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

Upvotes: 7

Related Questions