12

所以,我有一个UITextField子类,它是它自己的Delegate,并且在使用键盘快捷键时崩溃。它最大化CPU并且不会给出错误。如果不按原样分配自己Delegate,它就可以毫无问题地工作。当它按原样分配时Delegate,即使不使用任何(可选)方法,它也会崩溃。

自己试试:

子类UITextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.delegate = self;

    }
    return self;
}

它应该崩溃。

编辑 回溯:

* thread #1: tid = 0x3bb8d, 0x39f14726 libobjc.A.dylib`lookUpImpOrNil + 18, queue = 'com.apple.main-thread, stop reason = signal SIGSTOP
    frame #0: 0x39f14726 libobjc.A.dylib`lookUpImpOrNil + 18
    frame #1: 0x39f0dcb6 libobjc.A.dylib`class_respondsToSelector + 34
    frame #2: 0x39f1d05c libobjc.A.dylib`-[NSObject respondsToSelector:] + 32
    frame #3: 0x323b9242 UIKit`-[UITextField respondsToSelector:] + 46
    frame #4: 0x325c88a2 UIKit`-[UITextField customOverlayContainer] + 50
    frame #5: 0x325c8730 UIKit`-[UITextField automaticallySelectedOverlay] + 28
    frame #6: 0x32554208 UIKit`-[UIKeyboardImpl inputOverlayContainer] + 424
    frame #7: 0x32553942 UIKit`-[UIKeyboardImpl _autocorrectPromptRects] + 454
    frame #8: 0x323c7530 UIKit`-[UIKeyboardImpl updateAutocorrectPrompt:] + 368
    frame #9: 0x323e63e0 UIKit`-[UIDelayedAction timerFired:] + 80
    frame #10: 0x305fbe6c Foundation`__NSFireTimer + 64
    frame #11: 0x2fbe1e86 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
    frame #12: 0x2fbe1aa2 CoreFoundation`__CFRunLoopDoTimer + 794
    frame #13: 0x2fbdfe2a CoreFoundation`__CFRunLoopRun + 1218
    frame #14: 0x2fb4a540 CoreFoundation`CFRunLoopRunSpecific + 524
    frame #15: 0x2fb4a322 CoreFoundation`CFRunLoopRunInMode + 106
    frame #16: 0x348812ea GraphicsServices`GSEventRunModal + 138
    frame #17: 0x324011e4 UIKit`UIApplicationMain + 1136
    frame #18: 0x0009929c Today`main(argc=1, argv=0x27d79c80) + 164 at main.m:29
4

3 回答 3

13

我今天在使用自委托UITextField子类时遇到了同样的问题。如果无法将委托更改为 self 以外的其他内容,我建议您重写respondsToSelector而不是实现customOverlayContainer可能返回无效对象的方法(您确定它应该是的实例UITextField吗?您怎么知道 iOS 不是要求一个UIView或一个CGLayer或任何其他类型的对象?)

-(BOOL) respondsToSelector:(SEL)aSelector {

    NSString * selectorName = NSStringFromSelector(aSelector);

    if ([selectorName isEqualToString:@"customOverlayContainer"]) {

        NSLog(@"preventing self.delegate == self crash");

        return NO;
    }

    return [super respondsToSelector:aSelector];
}
于 2014-10-17T17:21:59.797 回答
5

通过将委托更改为初始化我的文本视图的对象来解决问题。

于 2013-12-14T20:43:28.077 回答
1

我通过自定义 customOverlayContainer解决了这个问题并返回了 textFiled。例如。

@implementation CustomTextField

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (id)customOverlayContainer {
    return self;
}

@end

在 iOS 7 和 iOS 6 上测试。

于 2014-07-03T11:36:21.377 回答