UITextField delegate jumping to 100% CPU usage and crashing upon using keyboard shortcut

懵懂的女人 提交于 2020-01-29 04:40:26

问题


So, I have a UITextField subclass which is it's own Delegate and is crashing when keyboard shortcuts are used. It maxes out on CPU and doesn't give an error. Without assigning itself as it's Delegate, it works without problem. When it is assigned as it's Delegate, it crashes even when using none of the (optional) methods.

Try it yourself:

Subclass UITextField.

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

        self.delegate = self;

    }
    return self;
}

It should crash.

EDIT Backtrace:

* 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

回答1:


I encountered the same issue today when working with a self-delegating UITextField subclass. If it is not possible to change the delegate to something other than self, I would recommend overriding respondsToSelector rather than implementing a customOverlayContainer method that may return an invalid object (are you sure it's supposed to be an instance of UITextField? How do you know iOS isn't asking for a UIView or a CGLayer or any other type of object?)

-(BOOL) respondsToSelector:(SEL)aSelector {

    NSString * selectorName = NSStringFromSelector(aSelector);

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

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

        return NO;
    }

    return [super respondsToSelector:aSelector];
}



回答2:


Problem solved by changing the delegate to the object which inits my text view.




回答3:


I solved this by custom customOverlayContainer and return the textFiled. eg.

@implementation CustomTextField

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

- (id)customOverlayContainer {
    return self;
}

@end

Tested on iOS 7 and iOS 6.



来源:https://stackoverflow.com/questions/19758025/uitextfield-delegate-jumping-to-100-cpu-usage-and-crashing-upon-using-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!