eshalev
eshalev

Reputation: 1

iphone: unrecognized selector sent to instance

I am new to objective C, So maybe there is some basic thing that I am missing about selectors. I would like to understand the basic concept behind this error as I have not found an general error reference.

I am getting this error when using:

[CloseButton addTarget:PageContents action:@selector(CloseButtonPressed) forControlEvents:UIControlEventTouchUpInside];

and then later:

- (void)CloseButtonPressed:(id)sender{
   UIAlertView *someError = [[UIAlertView alloc] initWithTitle: @"Comment" message: @"hello" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
   [someError show];
   [someError release];
}

Upvotes: 0

Views: 494

Answers (2)

Alex Reynolds
Alex Reynolds

Reputation: 96927

A couple suggestions that will help your code follow writing conventions used by all Objective C applications, and make your code more easily readable to others:

  1. Object instances should be lower case, i.e. closeButton and not CloseButton, and pageContents, not PageContents
  2. Method names should be lower case, i.e. -closeButtonPressed: and not -CloseButtonPressed:

To answer your question, you need to fix the action you are adding:

[CloseButton addTarget:PageContents action:@selector(CloseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

That colon character (:) makes sure the sender gets through to -CloseButtonPressed:

Upvotes: 3

Vladimir
Vladimir

Reputation: 170809

As CloseButtonPressed takes parameter you should create selector using: @selector(CloseButtonPressed:)

Upvotes: 0

Related Questions