NSCFString 还是 UIViewController?
我使用 UIViewController (当然是一个子类)和一个文本字段,当内容更改时发送一个操作(到 contentChanged: ViewController 的选择器)。这是通过将 contentChanged: 发送到 IB 中的文件所有者来完成的。 但是当我测试它时,它说:“-[NSCFString contentChanged:]:无法识别的选择器发送到实例”和十六进制的实例指针。 我猜测由于某种原因视图控制器被移动到另一个指针并且在那里分配了一个字符串,但我不明白为什么。 有什么想法吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来像是一个经典案例。阅读 NSZombieEnabled 了解如何追踪此类问题。
Sounds like a classic case. Read up on NSZombieEnabled for how to track this sort of problem down.
我对 UIViewController 的子类和这段无害的代码有完全相同的问题:
是的,answerButton 已连接(它是 IBOutlet),是的,
- (IBAction)getAnswerToQuestion:(id)sender;
是方法得当,却没有喜乐。当我注释掉 viewDidLoad 并在 IB 中建立连接时,崩溃报告中显示故障发生在 [UIControl sendAction:to:forEvent:] 上,导致我无法证明这一点,但我怀疑 UIKit 中的某个位置存在一个错误,它将绑定和 addTarget 转换为对 PerformSelector 的调用。我打算先升级到 iOS 4.01,看看是否能解决问题。
更新:
我不再确定我的问题是否真的与 Alexandre Cassagne 的问题相似,但为了共享信息,我暂时不会删除它。当我开始制作一个示例项目以提交错误报告时,我解决了我的问题,就像经常发生的那样。是的,单击使得answerButton调用getAnswerToQuestion:就像一个好的小对象一样,一切都很好。
示例项目的子类 UIViewController 与我的真实项目的子类之间的区别在于,第一个也充当 xib 的文件所有者,而第二个只是多个视图控制器之一。当我将 getAnswerToQuestion: 移至真实项目中的文件所有者时,单击answerButton 按预期工作。因此,我的预感是问题出在从绑定到执行选择器的转换中的某个地方,这一点并不遥远:问题出在响应者链上。我认为以编程方式或在 IB 中建立操作-目标链接会绕过响应者链,但显然不会。
当然,现在的问题是 Alexandre 在他的问题中指出他的contentsChanged: 方法已经是文件所有者的一部分,这使得我的答案与问题无关。
I have the exact same problem with a subclass of UIViewController and this piece of innocuous code:
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 inI 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.
不看代码,看起来您正在对文本字段的文本调用contentsChanged:,而不是UIViewController 子类。
without looking at the code, it looks like you are calling contentsChanged: on the text field's text, instead of the UIViewController subclass.
您应该考虑使用 UITextFieldDelegate 协议在 UITextField 的文本更改时进行回调。我没有看过,但这是我会立即做的事情。
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.