美文网首页
iOS Trapped uncaught exception '

iOS Trapped uncaught exception '

作者: 豪冷 | 来源:发表于2019-06-25 11:39 被阅读0次

崩溃

Trapped uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x110ff5c10> should have parent view controller:<MHRewardVoiceDetailVC: 0x110e71010> but requested parent is:<UIInputWindowController: 0x104380400>'


操作

在一个自定义的UILabel子类中
添加了一个长按事件
事件会弹出系统的UIMenuController
长按时
崩!!!


代码:

@implementation JHLongPressCopyLabel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressActioin:)]];
    }
    return self;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

#pragma mark - private
- (void)longPressActioin:(UIGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        UIView *view = gesture.view;
        [view becomeFirstResponder];
        
        UIMenuController *menuCtrl = [UIMenuController sharedMenuController];
        
        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyAction:)];
        menuCtrl.menuItems = @[copyItem];
        
        [menuCtrl setTargetRect:view.frame inView:view.superview];
        [menuCtrl setMenuVisible:YES animated:YES];
    }
}

- (void)copyAction:(UIMenuController *)menuCtrl
{
    [UIPasteboard generalPasteboard].string = self.text;
}

@end

问题

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

与方法- (void)longPressActioin:(UIGestureRecognizer *)gesture 内的
[view becomeFirstResponder];

导致了崩溃!!!

二选一
或者
都不选

即可!


额外的问题

唤起的菜单
如果不去点击菜单
而是去点击其他按钮
则菜单会一直显示
直到你点了TA!


长按复制内容

https://github.com/xjh093/JHLongPressCopy


一行代码召唤一个有意思的HUD

https://github.com/xjh093/JHAlertView


相关文章

网友评论

      本文标题:iOS Trapped uncaught exception '

      本文链接:https://www.haomeiwen.com/subject/rcltcctx.html