Adri
Adri

Reputation: 243

How to call a method

I have a method with the signature - (void)addStringsToArray. I want to call it in the viewDidLoad method. How do I call it?

Upvotes: 0

Views: 100

Answers (3)

user142019
user142019

Reputation:

Method calls (or, actually, message sends) in Objective-C have the syntax [receiver selector].

- (void)viewDidLoad {
  // Where MyClass is the class your method is in.
  MyClass *object = [[MyClass alloc] init];
  [object addStringsToArray];
}

Here, object is the receiver, and addStringsToArray is the selector. Use self as the receiver if your method is in the same class as the current method (i.e. your view controller).

I highly recommend you to read The Objective-C Programming Language. The answer to your question is under "Object Messaging" in the first chapter.

Upvotes: 4

Janardan Yri
Janardan Yri

Reputation: 751

I'm presuming that your confusion is because the method you want to call is inside the same UIViewController class. In that specific case, you're looking for 'self'.

[self addStringsToArray];

Upvotes: 1

Shmidt
Shmidt

Reputation: 16674

If you have wrote this method in same implementation file of your ViewController earlier:

[self addStringsToArray];

Upvotes: 0

Related Questions