Greg
Greg

Reputation: 57

Getting an runtime exception using addTarget:Action:forControlEvents

I am trying to use addTarget:Action:forControlEvents but I am receiving a runtime exception for an unrecognized selector.

This is being called from a UIView subclass in initWithFrame. Below is my code:

myButton = [[UIButton alloc] initWithFrame:frame];
[myButton addTarget:self action:&selector(displayList:)
                         forControlEvents:UIControlEventTouchUpInside];

My method:

-(void)displayList:(id)sender

When I click on the button it receive the following message:

-[MyClass displayList:]: unrecognized selector sent to instance. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[MyClass displayList:]: unrecognized selector sent to instance.

MyClass is a custom control that is a UIView subclass with a UIButton and UILabel. This class is going to be placed on a ViewController of another application.

I'm not sure what I am missing. Any help would be greatly appreciated.

Upvotes: 0

Views: 600

Answers (2)

graver
graver

Reputation: 15213

[myButton addTarget:self action:&selector(displayList:) forControlEvents:UIControlEventTouchUpInside];

The syntax is @selector(displayList:)
Also, there is a change your button may not fire the event. Try creating it this way:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //this will return an autoreleased UIButton object, be careful.
button.frame = CGRectMake(...);
...

Upvotes: 0

Frank
Frank

Reputation: 3376

it should read

[myButton addTarget:self action:@selector(displayList:) forControlEvents:UIControlEventTouchUpInside];

@ instead of &

Upvotes: 2

Related Questions