Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 468
Naeim Fard Avatar asked Feb 28 '10 07:02

Naeim Fard


People also ask

What is instance in Objective-C?

In Objective-C, there are instances, which are the objects that you create and use, and there are (semi-hidden) objects which are class objects, and which are created by the compiler. The class object is where the methods for the class are stored; each instance holds only its own data (i.e., instance variables).

When to use class method vs instance method Objective-C?

Instance methods are the most common type of method, because you'll most commonly be telling a specific object to perform some action. Class methods can be useful, however, when there's no contextual data needed in the method, and it can theoretically be implemented and run on any generic object.


1 Answers

[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];
like image 100
kennytm Avatar answered Oct 18 '22 00:10

kennytm