文本成为FirstResponder引发EXC

编程入门 行业动态 更新时间:2024-10-09 20:29:13
本文介绍了文本成为FirstResponder引发EXC_BAD_ACCESS代码= 1地址= 0x1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个问答,希望可以帮助遇到此问题的其他任何人.当我在控件之间转移FirstResponder链时,该问题似乎是随机发生的.

控件是从UIView继承的自定义控件对象,其中添加了UITextField或UITextView作为子视图(与其他控件一起使用),并且完全通过代码生成.

并不是所生成的控件的每个实例都引发异常,但是无论textfield/textview是否在代码中调用了[text becomeFirstResponder]还是点击了textfield/textview,该控件始终执行该异常.

在这个头发上,我把头发扯了大约6个小时...

解决方案

基本自定义控件的布尔值"firstResponder"是我用来跟踪屏幕上哪个自定义控件应该首先开始编辑的. /p>

初始化代码如下:

if([firstResponderAttribute isEqualToString:@"YES"]) firstResponder=YES; else firstResponder=NO;

当firstResponderAttribute为YES时,该控件引发错误,当为NO时,该控件正常.显然,将"firstResponder"设置为NO(== 0x0)时,响应者链将值视为nil,但是当将其设置为YES(错误消息中== 0x1)时,响应者链则试图访问地址处的对象0x1并失败.

解决方案?不要在任何UIView子类中将firstResponder用作值!

顺便说一句,Analyze并未解决此问题,而且我在Apple文档中也没有找到任何对该值的引用(尽管我看起来并不难...)

This is a Q&A to hopefully help anyone else who runs into this issue. The problem occurred seemingly at random while I was working on transferring the FirstResponder chain between controls.

The controls were custom control objects inherited from UIView with either a UITextField or UITextView added as a subview (with other controls) and generated entirely in code.

Not every instance of the controls that were generated threw the exception, but the ones that did did so consistently whether the textfield/textview had [text becomeFirstResponder] called in code, OR the textfield/textview was tapped on.

I was tearing my hair out for about 6 hours on this one...

解决方案

The base custom control had a boolean value 'firstResponder' which I was using to keep track of which of the custom controls on a screen should start editing first.

In the initialisation code was the following:

if([firstResponderAttribute isEqualToString:@"YES"]) firstResponder=YES; else firstResponder=NO;

When the firstResponderAttribute was YES the control threw the error, when it was NO the control was fine. Evidently when setting 'firstResponder' to NO (==0x0) the responder chain was seeing the value as nil, but when it was set to YES (==0x1 from the error message) the responder chain was trying to access an object at address 0x1 and failing.

Solution? Don't use firstResponder as value in any UIView subclass!

On a side note this problem wasn't picked up by Analyse and I haven't found any reference to the value in Apple docs (although I haven't looked very hard...)

更多推荐

文本成为FirstResponder引发EXC

本文发布于:2023-11-26 19:20:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   FirstResponder   EXC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!