欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS13適配三指撤銷和文案限長實例詳解

 更新時間:2023年01月29日 14:12:44   作者:yuec  
這篇文章主要為大家介紹了iOS13適配三指撤銷和文案限長實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

正文

在適配iOS13的過程中,UITextField輸入中文的時候三指撤銷產(chǎn)生了 crash。

Bugly報錯

NSInternalInconsistencyException
setGroupIdentifier:: _NSUndoStack 0x1206532f0 is in invalid state, calling setGroupIdentifier with no begin group mark

堆棧信息

 CoreFoundation	___exceptionPreprocess + 220
 libobjc.A.dylib	objc_exception_throw + 56
 Foundation	-[_NSUndoStack groupIdentifier]
 Foundation	-[NSUndoManager undoNestedGroup] + 240
 UIKitCore	-[UIUndoGestureInteraction undo:] + 72
 UIKitCore	-[UIKBUndoInteractionHUD performDelegateUndoAndUpdateHUDIfNeeded] + 96
 UIKitCore	-[UIKBUndoInteractionHUD controlActionUpInside:] + 152
 UIKitCore	-[UIApplication sendAction:to:from:forEvent:] + 96
 xxxxx	-[UIApplication(MemoryLeak) swizzled_sendAction:to:from:forEvent:] + 288
 UIKitCore	-[UIControl sendAction:to:forEvent:] + 240
 UIKitCore	-[UIControl _sendActionsForEvents:withEvent:] + 408
 UIKitCore	-[UIControl touchesEnded:withEvent:] + 520
 UIKitCore	-[UIWindow _sendTouchesForEvent:] + 2324
 UIKitCore	-[UIWindow sendEvent:] + 3352
 UIKitCore	-[UIApplication sendEvent:] + 336
 UIKitCore	___dispatchPreprocessedEventFromEventQueue + 5880
 UIKitCore	___handleEventQueueInternal + 4924
 UIKitCore	___handleHIDEventFetcherDrain + 108
 CoreFoundation	___CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
 CoreFoundation	___CFRunLoopDoSource0 + 80
 CoreFoundation	___CFRunLoopDoSources0 + 180
 CoreFoundation	___CFRunLoopRun + 1080
 CoreFoundation	CFRunLoopRunSpecific + 464
 GraphicsServices	GSEventRunModal + 104
 UIKitCore	UIApplicationMain + 1936
 xxxxx	main + 148
 libdyld.dylib	_start + 4

問題定位

沒有太多思路的時候,通過注釋代碼,最終定位到了問題所在。

[self addTarget:observer
         action:@selector(textChange:)
forControlEvents:UIControlEventEditingChanged];
- (void)textChange:(UITextField *)textField {
    ... ... 
    UITextRange *selectedRange = [textField markedTextRange];
    if (!selectedRange || !selectedRange.start) {
        if (destText.length > maxLength) {
            textField.text = [destText substringToIndex:maxLength];
        }
    }
}

這段代碼在輸入的時候會限制文案的長度。三指撤銷會觸發(fā)UIControlEventEditingChanged事件,執(zhí)行textChange,此時獲取到的markedTextRangenil,即便是存在markedText。這就導致UITextFieldtext有可能會被修改。修改文案后再繼續(xù)執(zhí)行撤銷操作,必定會產(chǎn)生 crash。

解決方案

將文案判長和截取異步添加到主隊列,在下一個runloop執(zhí)行。

- (void)textChange:(UITextField *)textField {
    dispatch_async(dispatch_get_main_queue(), ^{
        ... ...
    });
}

數(shù)字截斷后 crash

數(shù)字輸入限制長度后,超過長度后繼續(xù)輸入,這個時候撤銷也會產(chǎn)生crash,而且上面的方法不可行。目前想到的方案是在UITextField的回調方法進行輸入的攔截。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    /// 輸入數(shù)字后截取字符串仍舊可以觸發(fā)撤銷操作導致crash, 在這里攔截一下
    if (textField.keyboardType == UIKeyboardTypeNumberPad
        && range.location >= textField.tt_maxLength) {
            return NO;
    }
    return YES;
}

以上就是iOS13適配三指撤銷和文案限長實例詳解的詳細內容,更多關于iOS13適配三指撤銷文案限長的資料請關注腳本之家其它相關文章!

相關文章

最新評論