我使用自定义的UITextField接收EXC_BAD_ACCESS。 如何追踪这个问题?

我正在编写适用于iOS 6和7的应用程序,这些应用程序将运行iPhone和iPad。 控件的呈现迫使我使用自定义的UITextField类 – 我需要缩进文本(我有2个文本字段与图像里面),我需要改变当前编辑的文本框的边框颜色。

我在互联网上find例子,并创build以下类:

interface

 #import <QuartzCore/QuartzCore.h> #import <Foundation/Foundation.h> @interface PEStfCustomTextField : UITextField<UITextFieldDelegate> @property (nonatomic) int maxSymbols; @property (nonatomic) int leftIndent; @end 

implementation

 #import <QuartzCore/QuartzCore.h> #import "PEStfCustomTextField.h" @implementation PEStfCustomTextField //- (id)initWithFrame:(CGRect)frame //{ // if (self = [super initWithFrame:frame]) { // self.delegate = self; // } // return self; //} #pragma mark methods // ------------------------------------------------------ - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ // Change border of text field that is editing to orange textField.layer.masksToBounds=YES; textField.layer.borderColor=[[UIColor orangeColor] CGColor]; textField.layer.borderWidth= 1.0f; UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _leftIndent, 10)]; [self setLeftViewMode:UITextFieldViewModeAlways]; [self setLeftView:spacerView]; return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField{ } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ // Change border of text field that is edited to black textField.layer.masksToBounds=YES; textField.layer.borderColor=[[UIColor blackColor] CGColor]; textField.layer.borderWidth= 1.0f; return YES; } - (void)textFieldDidEndEditing:(UITextField *)textField{ } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ // Restrict number of symbols in text field to "maxSymbols" NSUInteger oldLength = [textField.text length]; NSUInteger replacementLength = [string length]; NSUInteger rangeLength = range.length; NSUInteger newLength = oldLength - rangeLength + replacementLength; BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound; return newLength <= (int)_maxSymbols || returnKey; } - (BOOL)textFieldShouldClear:(UITextField *)textField{ return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField{ return YES; } // ------------------------------------------------------ #pragma mark inits // ------------------------------------------------------ // Custom init - (id)initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if (self) { self.layer.borderColor=[[UIColor grayColor] CGColor]; self.delegate = self; } return self; } // ------------------------------------------------------ @end 

我有3个字段,只有文本,他们工作正常。 在其他视图控制器中,我有另外两个文本字段可以填充任何文本。 当我开始填充他们的第二或第三或第四个字母填充时,我收到EXC_BAD_ACCESS (code=2, address=0xbf7ffffc)

我有每个字段的出口: @property (weak, nonatomic) IBOutlet PEStfCustomTextField *txtCustom;

我初始化文本字段:

 _txtCustom.maxSymbols = 15; _txtCustom.leftIndent = 10; 

我看的代码和初始化看起来很好给我。 你能告诉我如何find并解决问题吗? 我不知道问题来自哪里。

编辑:从堆栈跟踪…

 * thread #1: tid = 0x4af98, 0x0182e0b4 libobjc.A.dylib`lookUpImpOrForward + 9, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0xbf7ffffc) frame #0: 0x0182e0b4 libobjc.A.dylib`lookUpImpOrForward + 9 frame #1: 0x0182e05c libobjc.A.dylib`lookUpImpOrNil + 62 frame #2: 0x0182584a libobjc.A.dylib`class_respondsToSelector_inst + 65 frame #3: 0x0183846d libobjc.A.dylib`-[NSObject respondsToSelector:] + 58 frame #4: 0x00c0a7dc UIKit`-[UITextField respondsToSelector:] + 58 frame #5: 0x00c0f1a1 UIKit`-[UITextField customOverlayContainer] + 55 frame #6: 0x00c0f1b4 UIKit`-[UITextField customOverlayContainer] + 74 frame #7: 0x00c0f1b4 UIKit`-[UITextField customOverlayContainer] + 74 ... (this row appears many times) ... frame #261721: 0x00c0f1b4 UIKit`-[UITextField customOverlayContainer] + 74 frame #261722: 0x00c0f1b4 UIKit`-[UITextField customOverlayContainer] + 74 frame #261723: 0x00c060a6 UIKit`-[UITextField automaticallySelectedOverlay] + 36 frame #261724: 0x006da666 UIKit`-[UIKeyboardImpl inputOverlayContainer] + 459 frame #261725: 0x006dbdf2 UIKit`-[UIKeyboardImpl _autocorrectPromptRects] + 563 frame #261726: 0x006dc02f UIKit`-[UIKeyboardImpl updateAutocorrectPrompt:] + 390 frame #261727: 0x006dbb15 UIKit`-[UIKeyboardImpl updateAutocorrectPromptAction] + 143 frame #261728: 0x0183882b libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70 frame #261729: 0x00685469 UIKit`-[UIDelayedAction timerFired:] + 83 frame #261730: 0x0145cde7 Foundation`__NSFireTimer + 97 frame #261731: 0x01a65ac6 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22 frame #261732: 0x01a654ad CoreFoundation`__CFRunLoopDoTimer + 1181 frame #261733: 0x01a4d538 CoreFoundation`__CFRunLoopRun + 1816 frame #261734: 0x01a4c9d3 CoreFoundation`CFRunLoopRunSpecific + 467 frame #261735: 0x01a4c7eb CoreFoundation`CFRunLoopRunInMode + 123 frame #261736: 0x03ab65ee GraphicsServices`GSEventRunModal + 192 frame #261737: 0x03ab642b GraphicsServices`GSEventRun + 104 frame #261738: 0x004e6f9b UIKit`UIApplicationMain + 1225 * frame #261739: 0x000538bd PA-PAY`main(argc=1, argv=0xbfffed68) + 141 at main.m:16 

我想有一些无限循环,但我不明白为什么。 我注意到的一件事情 – 当我从模拟器的键盘填充数据时,我没有收到exception。 可能是这与隐藏键盘的代码连接。

问题是你把对象设置为自己的委托。 这不build议用于UITextFields。 如果您不想在视图控制器中使用委托代码,则可以将代码放在单独的委托类中。

如果你仍然想用你的方法,你可以添加下面的方法来解决这个问题:

 - (id)customOverlayContainer { return self; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { return [self applySimpleMaskOnTextfield:textField range:range replacementString:string]; } 

customOverlayContainer方法没有被苹果公司logging,所以可能会被拒绝。

也看到这里和这里 。