Tutorial :iphone: unrecognized selector sent to instance



Question:

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];  }  


Solution:1

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:


Solution:2

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »