UITextField在中文手寫輸入法下輸入崩潰的問題

項(xiàng)目中出現(xiàn)在UITextField輸入時(shí)程序崩潰,崩潰提示信息如下:
<Error>: CGContextSetFillColorWithColor: invalid context 0x0. Backtrace:
<<redacted>+72>
<<redacted>+328>
<<redacted>+28>
<<redacted>+1032>
<<redacted>+6760>
<<redacted>+520>
<<redacted>+532>
<<redacted>+1120>
<<redacted>+2636>
<<redacted>+1088>
<<redacted>+80>
<<redacted>+600>
<<redacted>+148>
<<redacted>+60>
<<redacted>+64>
<<redacted>+148>
<<redacted>+392>
<<redacted>+1060>
<<redacted>+3720>
<<redacted>+676>
<<redacted>+64>
<<redacted>+188>
<<redacted>+1196>
<<redacted>+148>
<<redacted>+292>
<<redacted>+32>
<<redacted>+252>
<<redacted>+512>
<<redacted>+324>
<<redacted>+32>
<<redacted>+372>
<<redacted>+1024>
<CFRunLoopRunSpecific+444>
<GSEventRunModal+180>
<<redacted>+684>
<UIApplicationMain+208>
<main+124>
1480921050.774281 hight_hitht:258.000000
libc++abi.dylib: terminate_handler unexpectedly threw an exception

這問題在模擬器上是沒有的,在真機(jī)上有時(shí)能重現(xiàn),有時(shí)又正常,相當(dāng)詭異,找了大半天還是沒有找到解決辦法,后來第二天早上突然被我找到是哪里的問題了。起初懷疑是xcode8.1的問題,因?yàn)檫@代碼一直工作得好好的,好像就是最近換了xcode8.1才出現(xiàn)這樣的問題。 后來證實(shí)我是我錯(cuò)怪了xcode8.1,雖然網(wǎng)上的確有不少人吐槽xcode8.1的各種bug。在英文和拼音輸入法下輸入是不會(huì)崩潰的,只是在中文手寫輸入法會(huì)崩潰; 原來是跟工程中的一個(gè)UIScrollView的category有關(guān),把下面的代碼去掉就正常,具體原來我也不是很清楚,應(yīng)該是手寫輸入法跟這個(gè)UIScrollView是有關(guān)系的。

最后不得不感嘆下,有的問題說不上是一個(gè)重要的知識(shí)點(diǎn),但是一旦遇到還是會(huì)卡上你不少時(shí)間。

import "UIScrollView+Touch.h"

@implementation UIScrollView (Touch)

// 下面的代碼有bug,會(huì)導(dǎo)致UITextField在使用手寫中文輸入的時(shí)候崩潰

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
    }

  • (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
    }

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容