开发者

NSCFString or UIViewController?

开发者 https://www.devze.com 2023-01-10 20:48 出处:网络
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 con

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 ge开发者_如何学Pythonts allocated there, but I cannot figure why. Any ideas ?


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号