iOS Trapped uncaught exception 'UIViewControllerHierarchyInconsistency'
原创
©著作权归作者所有:来自51CTO博客作者haocold的原创作品,请联系作者获取转载授权,否则将追究法律责任
崩溃
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