文本成为FirstResponder引发EXC_BAD_ACCESS代码= 1地址= 0x1 [英] text becomeFirstResponder throws EXC_BAD_ACCESS Code=1 Address=0x1

查看:85
本文介绍了文本成为FirstResponder引发EXC_BAD_ACCESS代码= 1地址= 0x1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

并不是所生成的控件的每个实例都引发异常,但是无论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_BAD_ACCESS代码= 1地址= 0x1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆