查看: 14|回覆: 0

[教程] iOS13适配三指撤销和文案限长实例详解

[複製鏈接]

3

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2011-3-8
發表於 2023-1-29 14:12:44 | 顯示全部樓層 |閲讀模式

正文

在适配iOS13的过程中,UITextField输入中文的时候三指撤销产生了 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];
        }
    }
}

这段代码在输入的时候会限制文案的长度。三指撤销会触发UIControlEventEditingChanged事件,执行textChange,此时获取到的markedTextRangenil,即便是存在markedText。这就导致UITextFieldtext有可能会被修改。修改文案后再继续执行撤销操作,必定会产生 crash。

解决方案

将文案判长和截取异步添加到主队列,在下一个runloop执行。

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

数字截断后 crash

数字输入限制长度后,超过长度后继续输入,这个时候撤销也会产生crash,而且上面的方法不可行。目前想到的方案是在UITextField的回调方法进行输入的拦截。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    /// 输入数字后截取字符串仍旧可以触发撤销操作导致crash, 在这里拦截一下
    if (textField.keyboardType == UIKeyboardTypeNumberPad
        && range.location >= textField.tt_maxLength) {
            return NO;
    }
    return YES;
}

以上就是iOS13适配三指撤销和文案限长实例详解的详细内容,更多关于iOS13适配三指撤销文案限长的资料请关注琼殿技术社区其它相关文章!

您可能感兴趣的文章:
  • IOS开发自定义Button的外观和交互行为示例详解
  • IOS开发Objective-C Runtime使用示例详解
  • iOS数据持久化UserDefaults封装器使用详解
  • iOS数据持久化KeyChain数据操作详解
  • iOS 16 CocoaAsyncSocket 崩溃修复详解
  • iOS开发蓝牙技术应用增加无线连接功能
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部