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
,此時獲取到的markedTextRange
是nil
,即便是存在markedText
。這就導致UITextField
的text
有可能會被修改。修改文案后再繼續(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適配三指撤銷文案限長的資料請關注腳本之家其它相關文章!
相關文章
使用Xcode為iOS應用項目創(chuàng)建PCH文件的方法及應用示例
這篇文章主要介紹了使用Xcode為iOS應用項目創(chuàng)建PCH文件的方法及應用示例,PCH文件可以保留應用的很多的基礎設置信息以供復用,需要的朋友可以參考下2016-03-03iOS 報clang: error: no input files錯誤的解決方法
這篇文章主要給大家介紹了關于iOS報clang: error: no input files錯誤的解決方法,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01實例解析iOS app開發(fā)中音頻文件播放工具類的封裝
這篇文章主要介紹了iOS app開發(fā)中音頻文件播放工具類的封裝,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01淺述iOS11 Xcode 9 按住command 單擊 恢復到從前(直接跳轉到定義)
這篇文章主要介紹了 iOS11 Xcode 9 按住command 單擊 恢復到從前(直接跳轉到定義)的相關資料,需要的朋友可以參考下2017-10-10iOS 中KVC、KVO、NSNotification、delegate 總結及區(qū)別
這篇文章主要介紹了iOS 中KVC、KVO、NSNotification、delegate 總結及區(qū)別的相關資料,需要的朋友可以參考下2016-10-10