5

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?

TechZen
  • 64,370
  • 15
  • 118
  • 145
N Fard
  • 1,063
  • 1
  • 15
  • 33
  • Maybe you'll find an answer [here.][1] [1]: http://stackoverflow.com/questions/3572448/objective-c-call-function-on-another-class – John Wilund Feb 15 '13 at 14:57
  • [This page][1] has also a valid answer. [1]: http://stackoverflow.com/questions/3572448/objective-c-call-function-on-another-class – John Wilund Feb 15 '13 at 15:02

1 Answers1

10
[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];
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005