views:

40

answers:

4

Hello, I am using UIViewController (a subclass of course) with a text field which sends an action when the contents changed (to the contentsChanged: selector of the ViewController). It is done by sending contentsChanged: to file's owner in IB. But when I test it, it says : "-[NSCFString contentsChanged:] : unrecognised selector sent to instance " and the instance pointer in hex. I am guessing that for some reason the view controller gets moved to another pointer and a string gets allocated there, but I cannot figure why. Any ideas ?

A: 

without looking at the code, it looks like you are calling contentsChanged: on the text field's text, instead of the UIViewController subclass.

Jesse Naugher
no i can show you the code where i give it a target :[inputTextField addTarget:self action:@selector(contentsChanged:) forControlEvents:UIControlEventEditingChanged];There is no reason for it to call the text !
Alexandre Cassagne
+1  A: 

I have the exact same problem with a subclass of UIViewController and this piece of innocuous code:

- (void)viewDidLoad
{
    NSLog(@"%@ %s %@", [self class], _cmd, answerButton);
    [self.answerButton addTarget:self
             action:@selector(getAnswerToQuestion:) 
       forControlEvents:UIControlEventTouchUpInside];
}

Yes, answerButton is connected (it's an IBOutlet), yes, - (IBAction)getAnswerToQuestion:(id)sender; is a proper method, but no joy. When I commented out the viewDidLoad and made the connection in IB, it showed in the crash report that the failure happens on [UIControl sendAction:to:forEvent:] resulting in

objc_msgSend() selector name: performSelector:withObject:withObject:

I can't prove it, but I suspect there's a bug somewhere in the UIKit that translates the bindings and addTarget to a call to performSelector. I'm planning to upgrade to iOS 4.01 first to see if that won't solve the problem.

UPDATE: I'm not sure anymore that my problem really is similar to Alexandre Cassagne's but in the interest of sharing information I will not delete it just yet. I solved my problem, as so often, when I started to make an example project in order to file a bug report. Yes, clicking made answerButton call getAnswerToQuestion: like a good little object and all was fine.

The difference between the subclassed UIViewController of the example project and that of my real project was that the first also functioned as the xib's File's Owner while the second was just one of several view controller. When I moved getAnswerToQuestion: to the File's Owner in my real project, clicking answerButton worked as expected. So, my hunch that the problem lay somewhere in the translation from binding to performSelector wasn't that far off: the problem lies in the Responder Chain. I would think that establishing the Action-Target link either programmatically or in IB would bypass the Responder Chain, but apparently not.

The problem now, of course, is that Alexandre states in his question that his contentsChanged: method already is part of the File's Owner, which makes my answer irrelevant to the question.

Elise van Looij
Thanks I'm gonna upgrade to 4.0.1 and see if it works. If you do before me, keep me posted here to tell me if it works.
Alexandre Cassagne
I just upgraded and I'm sorry to tell you it didn't work. I replaced (IBAction) with (void) which is the proper iOS form according to the docs. My latest crash report:iPhone Simulator 4.0 (211.1), iPhone OS 4.0.1 (iPhone/8A306)*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString getAnswerToQuestion:]: unrecognized selector sent to instance 0x711fec0'.I can confirm that "instance 0x711fec0" is the self specified in addTarget, i.e. the subclassed view controller.
Elise van Looij
+3  A: 

Sounds like a classic case. Read up on NSZombieEnabled for how to track this sort of problem down.

Mike Abdullah
This is definitely the first thing to try. If you hit a zombie, there's definitely a memory management problem.
Chuck
How does NSZombieEnabled work and what does it do ?
Alexandre Cassagne
How about searching for "NSZombieEnabled" to find out how? http://www.google.co.uk/search?q=NSZombieEnabled
Mike Abdullah
A: 

you should consider using the UITextFieldDelegate protocol to get called back when the text of a UITextField changes. I have not looked, but this is the thing I would do off the top of my head.

Cappuccino Plus Plus