N Fard
N Fard

Reputation: 1099

Objective-C: How to Call a Instance Method

I need to call a method I have defined as:

-(IBAction)next:(id)sender{ 
...

}

I want to call it in -[UIViewController viewDidload]

How can I call this method programmatically?

Upvotes: 5

Views: 6254

Answers (1)

kennytm
kennytm

Reputation: 523214

[self next:nil];
  • self is the object receiving the message, assuming -next: is defined in the same class as -viewDidLoad.
  • next: is the name of the message (method).
  • Since you don't use sender, pass the argument nil, meaning "nothing".

If -next: is defined in the App delegate but -viewDidLoad in some view controller, use

[UIApplication sharedApplication].delegate

to refer to the app delegate. So the statement becomes

[[UIApplication sharedApplication].delegate next:nil];

Upvotes: 11

Related Questions