UIControl子类点击显示UIMenu崩溃问题及实现需求
摘要
我想要实现一个UIControl子类,使其在点击时显示UIMenu,参考相关问题后,设置contextMenuInteractionEnabled为YES时出现崩溃。
代码与崩溃问题
自定义UIControl子类代码
@interface MyControl : UIControl @end @implementation MyControl @end
触发崩溃的代码
MyControl *myControl = [[MyControl alloc] init]; myControl.contextMenuInteractionEnabled = YES; //<-- 崩溃:Thread 1: "Invalid parameter not satisfying: interaction"
错误信息
*** Assertion failure in -[MyControl addInteraction:], UIView.m:17965
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: interaction'
错误堆栈包含CoreFoundation、libobjc.A.dylib等模块的调用信息。
疑问
- 错误信息"Invalid parameter not satisfying: interaction"是什么意思?如何修复?
- 我哪里操作错误?仅需实现点击自定义UIControl时显示菜单。
注:该代码在UIButton实例上可正常运行。
更新1:长按显示菜单的问题
按照方案修改代码后不再崩溃,但仅长按才显示菜单,希望实现类似UIButton的menu属性效果,点击即显示菜单:
MyControl *control = [[MyControl alloc] init]; control.showsMenuAsPrimaryAction = YES; UIContextMenuInteraction *contextMenuInteraction = [[UIContextMenuInteraction alloc] initWithDelegate:self]; [control addInteraction:contextMenuInteraction]; [self.view addSubview:control];
内容的提问来源于stack exchange,提问作者ashipma
相关产品推荐
相关产品推荐

