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.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.