How to use a Selector

Just a quick reminder before I forget (again) how selectors are called:

Some of Apple’s pre-written methods use whats known as a Selector. Here’s an example method that uses a selector:

// create a bar button item
UIBarButtonItem *linkButton = [[UIBarButtonItem alloc]initWithTitle:@"My Title" style:UIBarButtonItemStyleBordered target:self action:@selector(myMethod)];

Notice how the selector is created on the second line. All it wants to know here is the method name really. Imagine you had a method called myMethod in the current class (hence the use of self here), this is how it gets called via the action parameter.

If you were to call this method just by itself, you’d use the usual

[self myMethod];

For more information and how to build your own selectors, check out the Cocoa Core Competencies section on Selectors.

Leave a Reply